Dre*_*kes 257
ItemsControl除非您需要其他方面,否则您ListBox可以使用ItemsControl.它将物品放入ItemsPanel并且没有选择的概念.
<ItemsControl ItemsSource="{Binding MyItems}" />
Run Code Online (Sandbox Code Playgroud)
默认情况下,ItemsControl不支持其子元素的虚拟化.如果您有很多项目,虚拟化可以减少内存使用并提高性能,在这种情况下,您可以使用方法2并设置样式ListBox,或者为您的虚拟化添加虚拟化ItemsControl.
ListBox或者,只需设置ListBox的样式,使选择不可见.
<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>
Run Code Online (Sandbox Code Playgroud)
Asa*_*ani 149
我找到了一个非常简单直接的解决方案,我希望它能帮到你
<ListBox ItemsSource="{Items}">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
Wil*_*lka 25
您可以切换到使用ItemsControl而不是ListBox.一个ItemsControl没有选择的概念,所以没有什么可关闭.
Cal*_*ear 12
另一个值得考虑的选择是禁用ListBoxItems.这可以通过设置ItemContainerStyle来完成,如下面的代码片段所示.
<ListBox ItemsSource="{Binding YourCollection}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
如果您不希望文本为灰色,则可以通过使用以下键向样式的资源添加画笔来指定禁用的颜色:{x:Static SystemColors.GrayTextBrushKey}.另一种解决方案是覆盖ListBoxItem控件模板.
这也可以,如果我需要使用listbox而不是itemscontrol,但我只是显示了不应该选择的项目,我使用:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsHitTestVisible" Value="False" />
</Style>
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77986 次 |
| 最近记录: |