There is no ListBox.SelectionMode="None", is there another way to disable selection in a listbox?
Approach 1 - ItemsControl
Unless you need other aspects of the ListBox
, you could use ItemsControl
instead. It places items in the ItemsPanel
and doesn't have the concept of selection.
<ItemsControl ItemsSource="{Binding MyItems}" />
By default, ItemsControl
doesn't support virtualization of its child elements. If you have a lot of items, virtualization can reduce memory usage and improve performance, in which case you could use approach 2 and style the ListBox
, or add virtualisation to your ItemsControl
.
Approach 2 - Styling ListBox
Alternatively, just style the ListBox such that the selection is not visible.
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<!-- SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- SelectedItem without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- SelectedItem text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</ListBox.Resources>
I found a very simple and straight forward solution working for me, I hope it would do for you as well
<ListBox ItemsSource="{Items}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
You could switch to using an ItemsControl
instead of a ListBox
. An ItemsControl
has no concept of selection, so there's nothing to turn off.