Alexander Ventura
Alexander Ventura

Reputation: 1160

Hide items in a ListBox when ListBox is disabled

When a standard ListBox is disabled, the elements in it are simply grayed out. They are still visible, despite not being clickable. I would like to know if it is possible, and if it is, how to hide these items when the ListBox is disabled. I do not want to remove the elements from the ItemSource or create a temp storage for them.

So far, I thought about changing the visibility of the ListBox but that gets rid of the entire thing (including the border lines).

Upvotes: 0

Views: 252

Answers (2)

LPL
LPL

Reputation: 17063

You could bind Visibility of ListBoxItem to IsEnabled of ListBox

<ListBox ItemsSource="{Binding DataSource}">
    <ListBox.Resources>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Visibility" Value="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType=ListBox}, Converter={StaticResource BooleanToVisibilityConverter}}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Upvotes: 4

MrB
MrB

Reputation: 424

Just add another ListBox to the form and when you disable the first listbox with the elements inside of it, hide it, and then display the other to the user which will essentially be an empty greyed out listbox.

Then when you enable the box back, hide the empty one and show the real one.

Hope this helps.

Upvotes: 0

Related Questions