There is no ListBox.SelectionMode="None", is there another way to disable selection in a listbox?

Shimmy Weitzhandler picture Shimmy Weitzhandler · Sep 9, 2009 · Viewed 85k times · Source

How do I disable selection in a ListBox?

Answer

Drew Noakes picture Drew Noakes · Sep 9, 2009

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>