make Listbox items in WPF not selectable

WpfListbox

Wpf Problem Overview


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?

Wpf Solutions


Solution 1 - Wpf

If you don't need selection, use an ItemsControl rather than a ListBox

Solution 2 - Wpf

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>

Solution 3 - Wpf

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>

Solution 4 - Wpf

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.

Solution 5 - Wpf

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.

Solution 6 - Wpf

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;
    }
}

Solution 7 - Wpf

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>

Solution 8 - Wpf

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

Solution 9 - Wpf

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.

Solution 10 - Wpf

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

Solution 11 - Wpf

You can also handle PreviewMouseDown event

And to prevent tap you can set KeyboardNavigation.TabNavigation="None"

<ListView x:Name="Cards"
			.....
            PreviewMouseDown="CardMonthsDescriptors_OnPreviewMouseDown"
            KeyboardNavigation.TabNavigation="None"
>
...
private void Cards_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGhassan KarwchanView Question on Stackoverflow
Solution 1 - WpfThomas LevesqueView Answer on Stackoverflow
Solution 2 - WpfVadym KyrylkovView Answer on Stackoverflow
Solution 3 - WpfAsad DurraniView Answer on Stackoverflow
Solution 4 - Wpf4imbleView Answer on Stackoverflow
Solution 5 - WpfDenise DraperView Answer on Stackoverflow
Solution 6 - WpftornaciousView Answer on Stackoverflow
Solution 7 - Wpfuser1630939View Answer on Stackoverflow
Solution 8 - WpfvikyView Answer on Stackoverflow
Solution 9 - WpfAdrian BystrekView Answer on Stackoverflow
Solution 10 - WpfdbaView Answer on Stackoverflow
Solution 11 - WpfAl Banna Techno logyView Answer on Stackoverflow