Reputation: 1996
I have 3 controls on my page
ListBox A is databound to a collection of items A
ComboBox C is databound to a collection of items C
ListBox B is databound to a collection of items B
B has a reference to Item A and Item C, the ListBox B should only show the items where Item A is the selected item of ListBox A, and Item C is the selected item of ListBox C
i've messed a little with collectionviews as ItemSource on ListBox B, setting a filter, but I can only get it to update the content of ListBox B based on either ListBox A or ComboBox C, not them both at the same time.
Any ideas?
Upvotes: 1
Views: 749
Reputation: 96890
In your view model, give the CollectionView
a filter predicate, something like this:
Items = CollectionViewSource.GetDefaultView(_Items) as CollectionView;
Items.Filter = (x => ((Item)x).CategoryA == SelectedCategoryA
&& ((Item)x).CategoryC == SelectedCategoryC);
Bind the list/combo boxes' SelectedItem
to SelectedCategoryA
and SelectedCategoryC
properties. In the setters for those properties, call Items.Refresh()
.
Edit
In your list box, bind both ItemsSource
and SelectedItem
, e.g.
<ListBox ItemsSource="{Binding CategoryListA}"
SelectedItem="{Binding SelectedCategoryA, Mode=TwoWay}"/>
In your view model, create a property like this:
private Category _SelectedCategoryA;
public Category SelectedCategoryA
{
get { return _SelectedCategoryA; }
set
{
if (value != _SelectedCategoryA)
{
_SelectedCategoryA = value;
Items.Refresh();
}
}
}
Upvotes: 2
Reputation: 930
One solution would be to create a seperate collection of Item B from a public accessor property collection of type b. like this.
private List<B> m_trueCollection; //the actual collection of B
public ObservableCollection<B> FilteredB { get; set; } //bind to this
Then, listen in to whenever there is a change to the selected item of ComboBox C or Listbox A. using the SelectionChanged
Event property.
If a selection is changed, iterate the true collection and rebuild FilteredB according to your criteria.
Hope it helps.
Upvotes: 0