The idea is almost the same as in How to hide items of a combobox in WPF.
In my ComboBox
, I am showing multiple items, and I need to be able to hide irrelevant/incompatible items from the ComboBox
when an external flag is set (in my case it is a boolean Checkbox
with name ShowAllItems
).
My ComboBox
is bound to a list of items of a type which is "aware" of their compatibility status (a field named IsSupported
). I just need to hide/show these items depending on whether the Checkbox
is ticked or not.
A naïve approach would probably be adding/removing items from the list each time a Checkbox
's status is changed.
Another (also probably naïve approach) would be to add a new field to the items of the list IsVisible
, and then update this field each time a Checkbox
's status is changed.
To summarize, my filter would depend on an item's IsSupported
field and on the Checkbox
's ShowAllItems
field.
The ComboBox
:
<ComboBox ItemsSource="{Binding MyItemsList, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource {x:Type ComboBoxItem}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSupported}" Value="False">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
and the Checkbox
:
<CheckBox IsChecked="{Binding ShowAllItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Content="Show all items"/>