Reputation: 5343
I want to add an item to the combobox after binding it. for example:
this.cbCategory.ItemsSource = categoryList;
this.cbCategory.DisplayMemberPath = "CategoryName";
this.cbCategory.SelectedValuePath = "CategoryID";
i want to add("All", "%") as the first one.
Geetha.
Upvotes: 2
Views: 3915
Reputation: 62929
This is very simple using a CompositeCollection:
<ComboBox DisplayMemberPath="CategoryName" SelectedValuePath="CategoryID">
<ComboBox.ItemsSource>
<CompositeCollection>
<my:Item CategoryName="All" CategoryID="%" />
<CollectionContainer Collection="{Binding CategoryList}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
How it works: The CompositeCollection produes the "All" item followed by all of the items in the CategoryList collection. Note that <my:Item ... />
is the constructor for your item class. You will need to change it to your actual namespace and class name.
Important advice: I notice you are setting some ComboBox properties in code-behind. This is a very bad practice. You should use XAML as shown above.
Upvotes: 9
Reputation: 8032
You'll break the binding if you try to add it later and will no longer receive updates.
Why not just add your 'extra' item before you bind it?
Upvotes: 0