0

I have a WPF Listbox with AlternationCount=2.
I want to disable selection of the items in the box, but I want to keep the colors.

When I set IsEnabled="False" selection of the items in the listbox is disabled but the text and the background becomes gray.

How can I disable selection but keep the foreground color and alternation background color as if it was enabled?

skink
  • 5,133
  • 6
  • 37
  • 58
aengas
  • 155
  • 12

1 Answers1

0

there are a couple of properties in ListBoxItem which might help: Focusable and IsHitTestVisible. Set one of them to false to disable selection from view. (with Focusable="false" ListBoxItems will change color on mouse over). Setting IsEnabled="false" also prevents selection, but it additionally changes foreground color.

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Focusable" Value="False"/>
            <!--<Setter Property="IsHitTestVisible" Value="False"/>-->
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

note that selection still can be set by code using ListBox properties: e.g. SelectedIndex="3"

ASh
  • 34,632
  • 9
  • 60
  • 82