I have a ComboBox in a WPF application that is bound to an ObservableCollection of Department objects in a C# ViewModel class. I want to use the combo box to filter another collection by department (And indeed it works for that now) The problem is that I want to add an additional option "All" to the top of the list. Is there a correct way to do this. Making a fake department feels wrong in so many ways.
The ComboBox
<ComboBox ItemsSource="{Binding Path=Departments}"
SelectedValue="{Binding Path=DepartmentToShow , Mode=TwoWay}" />
On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.
When you select an item, it will be displayed on the textbox. We recommend that you execute the above example code and try some other properties and events of the combobox control.
To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.
You could use a CompositeCollection as the ItemsSource for the ComboBox to include the "All" option. You need to set the Collection property of the CollectionContainer to your "ObservableCollection of Department objects".
<ComboBox >
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>All</ComboBoxItem>
<CollectionContainer x:Name="departmentCollection"/>
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
Not sure if this will be suitable for your filtering situation however...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With