tags:

views:

1392

answers:

4

How to disable selection in a ListBox?

A: 

IsEnabled = false

Viktor Jevdokimov
It makes it all gray, this is not what I am looking for
Shimmy
But it's a straightforward answer to the straightforward question :)
Viktor Jevdokimov
A straight forward answer is this http://stackoverflow.com/questions/1398559/1398650#1398650, but thanks anyway
Shimmy
+5  A: 

Unless you need other aspects of the ListBox, you could just use ItemsControl. It'll just place items in the ItemsPanel and won't allow selection (or virtualization, IIRC.)

<ItemsControl ItemsSource="{Binding MyItems}" />

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>
Drew Noakes
This seems like a good approach. +1
baeltazor
doesn't work. the items are still selected.
Shimmy
no, it will only change the visual effect, not the actual selection behavior
Thomas Levesque
My main desire was the visual.
Shimmy
My first suggestion was to use ItemsControl. Did you miss that? :)
Drew Noakes
I tried the above and it works for me.
Drew Noakes
I did miss that...The above works, but the visual is between the selected and non-selected is different.Anyway thanks a lot.
Shimmy
+5  A: 

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.

Wilka
Charming.I never knew you could directly declare ItemsControl, I thought it's virtual (MustOverride), thanks!!!
Shimmy
you probably mean abstract (MustInherit)...
Thomas Levesque
You're right, thanks.
Shimmy
But would ItemsControl still render my items in one line?
Chry Cheng
@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
A: 

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

dartrax