问题描述
如何做我禁用选择在ListBox?
How do I disable selection in a ListBox?
推荐答案
除非你需要的列表框的其他方面
,你可以使用的ItemsControl
代替。它把项目中的 ItemsPanel
,没有选择的概念。
Approach 1 - ItemsControl
Unless you need other aspects of the ListBox
, you could use ItemsControl
instead. It places items in the ItemsPanel
and doesn't have the concept of selection.
<ItemsControl ItemsSource="{Binding MyItems}" />
在默认情况下,的ItemsControl
不支持其子元素的虚拟化。如果你有很多的项目,虚拟化技术可以减少内存使用,提高性能,在这种情况下,你可以使用方法2和风格的列表框
或
By default, ItemsControl
doesn't support virtualization of its child elements. If you have a lot of items, virtualization can reduce memory usage and improve performance, in which case you could use approach 2 and style the ListBox
, or add virtualisation to your ItemsControl
.
另外,不仅仅是样式列表框,这样的选择是不可见的。
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>
这篇关于没有ListBox.SelectionMode =&QUOT;无&QUOT;,有另一种方法来禁用选择的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!