Note that there are some explanatory texts on larger screens.

plurals
  1. PO"All" value for a Combobox Data Filter in WPF
    text
    copied!<p>During the construction of user control I populate the combobox with some data. Works fine. I have Status.ID as valuePath and Status.Name as displayPath.</p> <pre><code>cmb.ItemsSource = dbEntities.Status </code></pre> <p>The comobox will be used as a filter control and I need to insert some value for "All", which will be used as the empty filter.</p> <p>First I tried a funny solution:</p> <pre><code>ObjectSet objectSet= dbEntities.Status; Status stAll = new Status(); stAll.ID = -1; stAll.Name = "All"; objectSet.AddObject(stAll); cmb.ItemsSource = objectSet; </code></pre> <p>For some reason the object is not added to the objectSet. It didnt throw any exception either.</p> <p>Then I tried to insert it manually to the first index but I got the error: "Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead." My code looked like:</p> <pre><code>cmb.ItemsSource = entities.Status; cmb.Items.Insert(0,"All"); </code></pre> <p>Both didnt work. What would be the easiest way to add that line to the combobox? The error message got me confused. I am not sure how to use the ItemsSource for such a purpose. </p> <p>edit: I did not have enough rep to answer my own question so here is the working code. Thanks Craig again.</p> <pre><code>CompositeCollection comp = new CompositeCollection(); comp.Add(new CollectionContainer {Collection = dbEntities.Status}); Status stAll = new Status(); stAll.ID = -1; stAll.Name = "All"; comp.add(stAll); cmb.ItemsSource = comp; //do whatever filter you want when the selected value is -1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload