I have a listbox in WPF, and when they select an item, it shows an ugly colors Can I make all the items non-selectable?
-
1possible duplicate of [There is no ListBox.SelectionMode="None", is there another way to disable selection in a listbox?](http://stackoverflow.com/questions/1398559/there-is-no-listbox-selectionmode-none-is-there-another-way-to-disable-select) – Jony Adamit May 24 '15 at 09:12
-
3Never too late to order things up – Jony Adamit May 26 '15 at 03:38
-
Set Enabled="false" on the listbox. The opacity can be adjusted via CSS. – Jujucat Apr 05 '21 at 16:29
11 Answers
If you don't need selection, use an ItemsControl
rather than a ListBox

- 286,951
- 70
- 623
- 758
-
24Not always true; `ItemsControl` can't do some things that `ListBox` can that one might need, such as `ScrollIntoView` when using virtualisation. – Seth Carnegie Aug 25 '12 at 22:24
-
4Not necessarily true. There might be many reasons for not wanting to use the *original* selection mechanism of a ListBox but still retain the functionality: just to name one example, consider a ListBox of images where you want to add an extra checkbox in the corner of each image to enable selection. You will wire this checkbox to the original selection mechanism all right, still you want to disable the original click selection of the ListBox. – Gábor Mar 15 '15 at 13:51
Add Focusable property as false in ListBoxItem style:
<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
<!-- Possibly other setters -->
<Setter Property="Focusable" Value="False" />
</Style>

- 610
- 5
- 7
Please use this inside your listbox. I found this very elegant solution
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

- 2,197
- 1
- 15
- 12
If you dont want them selectable then you probably dont want a listview. But if this is what you really need then you can do it with a style:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border
Name="Border"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid>
<ListBox>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
</ListBox>
</Grid>
</Page>
Look at the IsSelected Trigger. You can make the border a different colour so it is not "Ugly" or set it to transparent and it will not be visible when selected.
Hope this helps.

- 13,979
- 15
- 70
- 125
There's an even easier way: set ListBox
property IsHitTestVisible="False"
. This prevents all the items in the list from receiving mouse events. This has the advantage of stopping the highlighting as you mouse-over as well.
It works for me in WP 7.1.

- 13,179
- 11
- 45
- 52

- 1,027
- 10
- 24
-
2
-
1@EladKatz: this is true. Which is why I sometimes end up adding my own `ScrollViewer` around the `ListBox` to re-establish scrolling. – Denise Draper Jan 23 '13 at 04:23
-
@DeniseDraper that's a good idea, but my scrollviewer doesn´t get "scrollable" when the list is full.. I know the content is enough for the scrollbar to work, but it just stays "inactive".. any thoughts? – StinkyCat Oct 17 '13 at 16:48
-
@StinkyCat: no ideas here, sorry. Weird things happening with scrolling are a frequent question topic on StackOverflow, though, so I doubt it has any specific connection to this situation. – Denise Draper Oct 24 '13 at 10:47
-
By far, the easiest and the most straight forward solution to present a listbox that is to have items that need to become un-selectable. – BoiseBaked Jul 25 '19 at 15:31
-
Came here needing a solution for nested listviews, this solution worked best for me. (The inner listview can't be an ItemsControl as it is a UserControl I am using elsewhere where I *do* need it to be selectable). – Danwizard208 Jan 16 '20 at 21:06
Better to avoid events, it's more elegant and without side effects the Style tag.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
... what you want as a source ...
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

- 99
- 5
A simple way to do this (using the answer from viky above) is to set the selected index to -1 in the SelectionChanged(), as follows.
public void OnListView_SelectionChanged(Object sender, RoutedEventArgs e)
{
if (null != sender && sender is ListView)
{
ListView lv = sender as ListView;
lv.SelectedIndex = -1;
}
}

- 31
- 1
you can handle SelectionChanged event of ListBox and unselect the selected item in the event handler.

- 17,275
- 13
- 71
- 90
You can also make disabled Listbox, which will give you static, non-interactive listbox.
<ListBox IsEnabled="False"/>
I think this is the solution as simple as possible.

- 528
- 2
- 6
- 19
In my case I had templated ListboxItems with a Textblock and a ComboBox. The only "active" should be the Combo...
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Disabled"
CanContentScroll="True" />
<ItemsControl>
....here my content....
</Itemscontrol>
</ScrollViewer>
did work for me as expected. BR, Daniel

- 1,159
- 1
- 14
- 22
You can also handle PreviewMouseDown
event
And to prevent tap you can set KeyboardNavigation.TabNavigation="None"
<ListView x:Name="Cards"
PreviewMouseDown="Cards_OnPreviewMouseDown"
KeyboardNavigation.TabNavigation="None"
>
...
private void Cards_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}

- 3,119
- 19
- 19
- 37

- 339
- 4
- 11