如何禁用列表框上的突出显示但保持选择? [英] How to disable highlighting on listbox but keep selection?
问题描述
我无法找到如何让我的列表框突出显示所选项目的方法.我知道我没有添加触发器来突出显示该项目.
I am having trouble finding how to not allow my ListBox to highlight the item selected. I know that I didn't add a trigger to highlight the item.
<ListBox Name="CartItems" ItemsSource="{Binding}"
ItemTemplate="{StaticResource TicketTemplate}"
Grid.Row="3" Grid.ColumnSpan="9" Background="Transparent"
BorderBrush="Transparent">
</ListBox>
推荐答案
迟到的答案,但有一个更好更简单的解决方案:
Late answer, but there's a much better and simpler solution:
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
</ListBox>
这允许您拥有一个看起来就像一个项目控件但支持选择的 LisBox.
This allows you to have a LisBox that looks just like an itemscontrol, but has support for selection.
工作原理
这会改变系统的颜色",即您的 Windows 主题,仅针对此 ListBox 及其子项(我们实际上想要针对 ListboxItem
).
例如,悬停 ListboxItem
通常会给它一个深蓝色背景,但在这里我们将其设置为透明 (HighlightBrushKey).
For example hovering a ListboxItem
usually gives it a deep blue background, but here we set it to transparent (HighlightBrushKey).
编辑(2016 年 6 月 30 日):
似乎对于最新的 Windows 版本这已经不够了,您还需要重新定义 InactiveSelectionHighlightBrushKey
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />
感谢@packoman 的评论
Thanks to @packoman in the comments
这篇关于如何禁用列表框上的突出显示但保持选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!