How do I disable selection in a ListBox?
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
.
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>