Phil
Phil

Reputation: 47

Hide ComboBox Item

I am using a ComboBox filled with some Userlevels:

full list

I also use a Converter which checks if a Userlevel is visible or not. Converter is working fine. But the items are still in the list, they are empty only:

filtered list

I use a TextBlock inside the Combobox to show the text. Here is my code:

<ComboBox x:Name="UserlevelComboBox" Width="300" Height="50"
               ItemsSource="{Binding Path=UserlevelList}"
               SelectedValue="{Binding Userlevel.Id}"
               SelectedValuePath="Id"
               SelectedItem="{Binding Userlevel}"
               IsSynchronizedWithCurrentItem="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Text}">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Userlevel, Converter={StaticResource userlevelConverter}}" Value="False">
                                <Setter Property="Visibility" Value="Collapsed"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Is it possible to get the Combobox Items look like this:

enter image description here

Thanks a lot. best regards Phil

Upvotes: 1

Views: 926

Answers (1)

ASh
ASh

Reputation: 35720

At the moment you are hiding only TextBlocks. But in ComboBox items are wrapped in container items - ComboBoxItem - which are still Visible.

Apply your DataTrigger in ComboBox.ItemContainerStyle.:

<ComboBox x:Name="UserlevelComboBox" Width="300" Height="50"
               ItemsSource="{Binding Path=UserlevelList}"
               SelectedValue="{Binding Userlevel.Id}"
               SelectedValuePath="Id"
               SelectedItem="{Binding Userlevel}"
               IsSynchronizedWithCurrentItem="True">
    <ComboBox.ItemContainerStyle>

        <Style TargetType="{x:Type ComboBoxItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Userlevel, Converter={StaticResource userlevelConverter}}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </ComboBox.ItemContainerStyle>
</ComboBox>

Upvotes: 2

Related Questions