开发者

WPF how to make a listbox/listview unfocusable

开发者 https://www.devze.com 2023-02-17 22:51 出处:网络
I\'ve been trying for a while to display some data in a listbox/listview that would be unfocusable (I mean not only the list, but also the items in it).

I've been trying for a while to display some data in a listbox/listview that would be unfocusable (I mean not only the list, but also the items in it).

I tried with both types of list (listbox and listview), and I used their ItemTemplate and ItemContainerStyle. Everywhere I could, I set the Focusable property to false.

I don't see any other way than disabling the list, but then I have to change all its style, to make it appear not disabled.

Have I missed something? Is there a read-only type of list that I don't know about?

Thank y开发者_JS百科ou for your ideas :)


The problem you're probably seeing is that each individual item in the list is focusable. However, you can override that... Try adding this to your listbox:

  <ListBox.ItemContainerStyle>
    <Style TargetType="Control">
      <Setter Property="Focusable" Value="False" />
    </Style>
  </ListBox.ItemContainerStyle>

Note however that this makes the items unselectable (by keyboard or by mouse). You can set the selected item programmatically, but it doesn't appear to be highlighted automatically any more - so really, this behaves almost the same as an ItemsControl.


Use an ItemsControl with TextBlocks instead of a ListBox

<ItemsControl ItemsSource="{Binding MyListBoxItemsSource}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MyDisplayName}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
0

精彩评论

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

关注公众号