tags:

views:

914

answers:

5

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?

+1  A: 

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"&gt;
  <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. Kohan

Kohan
A: 

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

viky
+4  A: 

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

Thomas Levesque
A: 

Thanks a lot Actually I didn't know about the ItemsControl I thought it was an abstract class. I didn't want the listbox, so I used the ItemsControl

gkar
A: 

use ItemsControl

Raj Chanchal