Reputation: 3360
I have a ComboBox
that displays some objects by using this simplified template:
<DataTemplate x:Key="ComboBoxDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Title}" Margin="10,0,2,0" VerticalAlignment="Center"/>
<TextBlock Grid.Row="1" Text="{Binding Description}" Margin="10,0,2,0"
TextTrimming="CharacterEllipsis" FontSize="11" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
The ComboBox
has predefined items (they extend from FrameworkElement
), but depending on other parameters, I need to hide some of these items.
<ComboBox VerticalAlignment="Center" Margin="10,3,5,3" Height="38" ItemTemplate="{StaticResource ComboBoxDataTemplate}">
<i:GenericItem x:Name="SomethingItem" Title="Something" Description="Abc" Value="{x:Static u:MyEnum.Something}"/>
<i:GenericItem x:Name="ElseItem" Title="Else" Description="Def" Value="{x:Static u:MyEnum.Else}"/>
<i:GenericItem x:Name="GoesItem" Title="Goes" Description="Ghj" Value="{x:Static u:MyEnum.Goes}"/>
<i:GenericItem x:Name="HereItem" Title="Here" Description="Ijk" Value="{x:Static u:MyEnum.Here}"/>
</ComboBox>
I'm wondering if there's a simple way, like setting IsEnabled
, to hide these items?
Without having to add/remove the items from the ComboBox
.
Upvotes: 1
Views: 38
Reputation: 169370
You could define an ItemContainerStyle
that sets the Visibility
property of the ComboBoxItem
container to Collapsed
based on some property value of a GenericItem
, e.g.:
<ComboBox VerticalAlignment="Center" Margin="10,3,5,3" Height="38" ItemTemplate="{StaticResource ComboBoxDataTemplate}">
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
<i:GenericItem x:Name="SomethingItem" Title="Something" Description="Abc" Value="{x:Static u:MyEnum.Something}"/>
<i:GenericItem x:Name="ElseItem" Title="Else" Description="Def" Value="{x:Static u:MyEnum.Else}"/>
<i:GenericItem x:Name="GoesItem" Title="Goes" Description="Ghj" Value="{x:Static u:MyEnum.Goes}"/>
<i:GenericItem x:Name="HereItem" Title="Here" Description="Ijk" Value="{x:Static u:MyEnum.Here}"/>
</ComboBox>
The above sample markup should hide any item that gets disabled.
Upvotes: 1