How do I disable selection in a ListBox?
-
1Could you provide an example where it is valid to have a ListBox that you can not select from? Since the main behavior is to select items. I would probably chose another way to display it.(This is not me trying to be a critic but rather an genuine interest in where this might occur) – Marthin Jan 17 '12 at 12:02
-
3@Martin: for instance if you wanted to drag content from a listboxitem - in this case you're probably not interested in selecting that item. ALSO: when dragging an item: selected item of listbox changes while you drag within the listbox - see this post http://stackoverflow.com/questions/7589142/why-does-the-wpf-listbox-change-selection-on-mouse-button-down-rather-than-butto – Danield May 15 '12 at 10:15
-
1I believe the reason that Shimmy wants to use ListBox is that the asker can make the listbox selectable sometime. The question is also valuable to me. Say you are building a playing card game. You can select one card from your cards, sometimes, you can select multiple and at other times, you cannot select any. – Gqqnbig Dec 18 '12 at 06:37
-
1Plus, sometimes you have 10 cards and only 4 of them are selectable. Among the 4, you can select up to 3. – Gqqnbig Dec 18 '12 at 06:44
-
1@Marthin: When you have a GridView in a ListBox. Gridview headers provide a lot of functionality that's not available elsewhere. And you have edit controls in the cells of the gridview. – Robin Davies Jun 13 '20 at 11:07
17 Answers
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>

- 1
- 1

- 300,895
- 165
- 679
- 742
-
27no, it will only change the visual effect, not the actual selection behavior – Thomas Levesque Sep 09 '09 at 10:00
-
-
10My first suggestion was to use ItemsControl. Did you miss that? :) – Drew Noakes Sep 09 '09 at 10:41
-
5Re-reading these comments again I want to point out that @Thomas Levesque's comment is only true of the second approach I show. Using plain `ItemsControl` will completely remove any concept of selection. – Drew Noakes Apr 20 '11 at 03:03
-
1ItemsControl solution remove out the box scrolling support (scrollbar and mousewheel). – MuiBienCarlota Apr 15 '13 at 09:08
-
2+1 for Approach 1 - ItemsControl. If we have a huge page which we have to scroll, if the user mouses over a ListBox, it effectively disables the MouseWheel as the listbox grabs the MouseWheel events. This means that the user gets frustrated that the mousewheel used to scroll the entire page will randomly stop working, depending on whether the mouse is over a listbox or not. – Contango Feb 05 '15 at 13:05
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>

- 2,197
- 1
- 15
- 12
-
I think he went through it quite well here: http://asaddurrani.wordpress.com/tag/wpf-listbox-disable-selection/ – Sid Apr 11 '13 at 09:13
-
3This is perfect. it prevent selected item and other controls like buttons still works. exactly what i was looking for – Franck Oct 18 '13 at 15:10
-
1+1 for this approach. If we have a huge page which we have to scroll, if the user mouses over a ListBox, it effectively disables the MouseWheel as the listbox grabs the MouseWheel events. This means that the user gets frustrated that the mousewheel used to scroll the entire page will randomly stop working, depending on whether the mouse is over a listbox or not. – Contango Feb 05 '15 at 13:13
-
Excellent. Similar approach also worked for me when I needed buttons on items not to cause item selection - but only if other area of the item was clicked. Simply set the buttons `Focusable = "False"`! – Jony Adamit Jan 25 '16 at 15:11
-
-
-
As Contago responded, this disabled scrolling making this not helpful. – user99999991 Aug 19 '17 at 00:42
-
5Add this additional property to remove the mouseover highlighting as well: `
` – DonBoitnott Dec 04 '17 at 13:42
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.

- 28,701
- 14
- 75
- 97
-
2Charming. I never knew you could directly declare ItemsControl, I thought it's virtual (MustOverride), thanks!!! – Shimmy Weitzhandler Sep 09 '09 at 09:56
-
-
@Chry yes it would, and in addition, you can always manually set the [`ItemTemplate`](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx "ItemTemplate"). – Shimmy Weitzhandler Oct 19 '10 at 09:28
-
3
-
@Jeff you can wrap the ItemsControl in a ScrollViewer to gain scrolling. – Wilka Feb 23 '12 at 14:47
-
You can wrap it in a scrollviewer, but you're losing virtualization that listbox has. – user99999991 Aug 19 '17 at 00:43
This will also work, if I have the need to use listbox instead of itemscontrol, but am just displaying the items which shouldn't be selectable, I use:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
</ListBox.ItemContainerStyle>

- 800
- 7
- 11
-
1That is the best answer! At least in my case of an existing complex style for list box items. I just extended it with this setter and it worked like a charm. Thank you! – Antiohia Nov 19 '20 at 10:24
-
Yup, this is perfect if you want to keep ListBox functionality and can't use an ItemControl – johnc Nov 03 '22 at 22:18
Another option worth considering is disabling the ListBoxItems. This can be done by setting the ItemContainerStyle as shown in the following snippet.
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
If you don't want the text to be grey you can specify the disabled color by adding a brush to the style's resources with the following key: {x:Static SystemColors.GrayTextBrushKey}. The other solution would be to override the ListBoxItem control template.

- 2,637
- 2
- 21
- 20
-
Simple and working, thanks! And it's applicable on WP 8.1 Runtime as well. – Malutek Sep 27 '14 at 11:07
Quite good answers here, but I was looking for something slightly different: I want selection, but just do not want it to be shown (or shown in a different matter).
The solutions above did not work for me (completely), so I did something else: I used a new style for my listbox, which completely redefines the templates:
<Style x:Key="PlainListBoxStyle" TargetType="ListBox">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<ItemsPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Starting with that, you can easily add you own selection highlighting, or leave it like that if you dont want any at all.

- 2,283
- 1
- 17
- 17
I propose yet another solution. Simply re-template ListBoxItem
to be nothing more than a ContentPresenter
, like so...
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My reasons for this approach are as follows:
In my case, I don't want to disable user interaction with the contents of my
ListBoxItems
so the solution to setIsEnabled
won't work for me.The other solution that attempts to re-style the
ListBoxItem
by overriding the color-related properties only works for those instances where you're sure the template uses those properties. That's fine for default styles, but breaks with custom styles.The solutions that use an
ItemsControl
breaks too many other things as theItemsControl
has a completely different look than a standardListBox
and doesn't support virtualization, meaning you have to re-template theItemsPanel
anyway.
The above doesn't change the default look of the ListBox
, doesn't disable items in the data templates for the ListBox
, supports virtualization by default, and works independently of whatever styles may or may not be in use in your app. It's the KISS principle.

- 28,442
- 25
- 137
- 286
While @Drew Noakes's answer is a quick solution for most cases there is a bit of a flaw that comes with setting the x:Static brushes.
When you set the x:Static brushes as suggested, all of the children controls within the list box item will inherit this style.
That means that, while this will work for disabling the highlighting of the list box item, it may result in undesired effects for the child controls.
For example, if you had a ComboBox within your ListBoxItem, it would disable the mouse over highlighting within the ComboBox.
Instead, consider setting the VisualStates for the Selected, Unselected, and MouseOver events as covered in the solution mentioned in this stackoverflow thread: Remove Control Highlight From ListBoxItem but not children controls.
-Frinny
A simple fix that works on Windows Phone for instance is on selection setting selected item to null:
<ListBox SelectionChanged="ListBox_SelectionChanged">
And in the code behind:
private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
(sender as ListBox).SelectedItem = null;
}

- 13,320
- 37
- 106
- 184
I found a perfect way.
Set ListBox IsHitTestVisible to false so that user can't mouse hover or scroll down or scroll up.
Capture PreviewGotKeyboardFocus e.Handled = true so that user can's select item by keyboard Tab, Arrow up, Arrow down.
This way advantage:
- ListBox items Foreground will not become Gray.
- ListBox Background can set to Transparent
xmal
<ListBox Name="StudentsListBox" ItemsSource="{Binding Students}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0" Background="Transparent" IsHitTestVisible="False" PreviewGotKeyboardFocus="StudentsListBox_PreviewGotKeyboardFocus">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="Bd" Property="Background" Value="Yellow" />
<Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Name="GradeBlock" Text="{Binding Grade}" FontSize="12" Margin="0,0,5,0"/>
<TextBlock Grid.Column="1" Name="NameTextBlock" Text="{Binding Name}" FontSize="12" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
code
private void StudentsListBox_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}

- 11
- 2
Maybe you need onlly functionality of ItemsControl? It don't allow selection:
<ItemsControl ItemsSource="{Binding Prop1}" ItemTemplate="{StaticResource DataItemsTemplate}" />

- 8,537
- 4
- 49
- 76
-
3@Shimmy: It is common for trivial answers to be similar. There is no duplication here worthy of any flag. If you have any more questions about this, please ask on [meta]. – Jul 07 '11 at 14:42
Note: This solution does not disable selection by keyboard navigation or right clicking (ie. arrow keys followed by space key)
All previous answers either remove the ability select completly (no switching in runtime) or simply remove the visual effect, but not the selection.
But what if you want to be able to select and show selection by code, but not by user input? May be you want to "freeze" the user's selection while not disabling the whole Listbox?
The solution is to wrap the whole ItemsContentTemplate into a Button that has no visual chrome. The size of the button must be equal to the size of the Item, so it's completely covered. Now use the button's IsEnabled-Property:
Enable the button to "freeze" the item's Selection-state. This works because the enabled button eats all mouse events before they bubble up to the ListboxItem-Eventhandler. Your ItemsDataTemplate will still receive MouseEvents because it's part of the buttons content.
Disable the button to enable changing the selection by clicking.
<Style x:Key="LedCT" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Button IsEnabled="{Binding IsSelectable, Converter={StaticResource BoolOppositeConverter}}" Template="{DynamicResource InvisibleButton}">
<ContentPresenter />
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="InvisibleButton" TargetType="{x:Type Button}">
<ContentPresenter/>
</ControlTemplate>
dartrax

- 11
- 3
For me best solution is:
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
</ListBox.ItemContainerStyle>

- 1,104
- 8
- 27
You can place a Textblock above your listbox, it will not change the look of your application and also it won't allow to select any item.

- 1,048,767
- 296
- 4,058
- 3,343

- 37
- 1
The solution should be simple and straight forward.
This one has several advantages:
- Keyboard navigation is also disabled. This is not the case with
IsFocusable
,IsHitTestVisible
, etc. - No visual cues of "disabled" elements: Only the
ListBoxItem
is disabled, however theTextBlock.Foreground
property sets the correct color.
Result: An item cannot be selected by keyboard or by mouse and the color is not "gray" because we don't disable an entire control.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Black" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

- 14,163
- 30
- 110
- 141
IsEnabled = false

- 1,054
- 6
- 13
-
2
-
1But it's a straightforward answer to the straightforward question :) – Viktor Jevdokimov Sep 09 '09 at 10:09
-
1A straight forward answer is this http://stackoverflow.com/questions/1398559/1398650#1398650, but thanks anyway – Shimmy Weitzhandler Aug 05 '10 at 18:00
-
To disable one or more options in your listbox/dropdown, you can add the "disabled" attribute as shown below. This prevent the user from selection this option, and it gets a gray overlay.
ListItem item = new ListItem(yourvalue, yourkey);
item.Attributes.Add("disabled","disabled");
lb1.Items.Add(item);

- 841
- 9
- 10