I'm creating a WPF application using the MVVM design pattern that consists of a ListView and some ComboBoxes. The ComboBoxes are used to filter the ListView. What I am trying to accomplish is populating the combobox with items in the related ListView column. In other words, if my ListView has Column1, Column2, and Column3, I want ComboBox1 to display all UNIQUE items in Column1. Once an item is selected in the ComboBox1, I want the items in ComboBox2 and ComboBox3 to be filtered based on ComboBox1's selection, meaning that ComboBox2 and ComboBox3 can only contain valid selections. This would be somewhat similar to a CascadingDropDown control if using the AJAX toolkit in ASP.NET, except the user can select any ComboBox at random, not in order.
My first thought was to bind ComboBoxes to the same ListCollectionView that the ListView is bound to, and set the DisplayMemberPath to the appropriate column. This works great as far as filtering the ListView and ComboBoxes together goes, but it displays all items in the ComboBox rather than just the unique ones (obviously). So my next thought was to use a ValueConverter to only return the only the unique items, but I have not been sucessful.
FYI: I read Colin Eberhardt's post on adding a AutoFilter to a ListView on CodeProject, but his method loops through each item in the entire ListView and adds the unique ones to a collection. Although this method works, it seems that it would be very slow for large lists.
Any suggestions on how to achieve this elegantly? Thanks!
Code Example:
<ListView ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct}">
<ListView.View>
<GridView>
<GridViewColumn Header="Item" Width="100" DisplayMemberBinding="{Binding ProductName}"/>
<GridViewColumn Header="Type" Width="100" DisplayMemberBinding="{Binding ProductType}"/>
<GridViewColumn Header="Category" Width="100" DisplayMemberBinding="{Binding Category}"/>
</GridView>
</ListView.View>
</ListView>
<StackPanel Grid.Row="1">
<ComboBox ItemsSource="{Binding Products}" DisplayMemberPath="ProductName"/>
<ComboBox ItemsSource="{Binding Products}" DisplayMemberPath="ProductType"/>
<ComboBox ItemsSource="{Binding Products}" DisplayMemberPath="Category"/>
</StackPanel>