开发者

make Listbox items in WPF not selectable

I have a listbox in WPF, and when they select an item, it shows an ugly colors Can I make al开发者_如何学Gol the items non-selectable?


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


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>


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>


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.


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.


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


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>


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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜