在本地资源字典中覆盖SystemColors [英] Overriding SystemColors in local Resources dictionary
问题描述
我试图隐藏一些视觉提示,这些提示指示在WPF ListBox
中的选择. 此答案建议通过覆盖SystemColors
表示ListBox
.
I'm trying to hide the visual hints that indicate selection in a WPF ListBox
. This answer suggests this should work by overriding the SystemColors
for that ListBox
.
我创建了一个新的WPF项目,并像这样编辑MainWindow.xaml
:
I created a new WPF project and edited the MainWindow.xaml
like this:
<Window x:Class="WpfListboxWithoutSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfListboxWithoutSelection"
xmlns:s="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="325">
<Grid>
<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>
<s:String>Item 1</s:String>
<s:String>Item 2</s:String>
<s:String>Item 3</s:String>
</ListBox>
</Grid>
</Window>
不幸的是,这不起作用,窗口显示如下:
Unfortunately this doesn't work, the window appears like this:
知道我在做什么错吗?如何删除所选项目和悬停的项目上出现的蓝色?
Any idea what I'm doing wrong? How can I remove the blue colors that appear on the selected item and on the one hovered?
推荐答案
肯定不会起作用. 由于ListBox.Resources将为ListBox设置资源,而不是为ListBoxItem设置资源. 所以这是一个解决方案:
definitely it wont work. Since ListBox.Resources will set a resource for ListBox not for ListBoxItem. So here is a solution:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
在windows.resource中添加这种样式,例如:
Attach this style in windows.resource like:
<Window.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</Window.Resources>
而且可以挑衅地将更多SolidColorBrush放入Style.Resources就像您在代码中所做的一样.
And defiantly you can put more SolidColorBrush into Style.Resources like what you did in your code.
这篇关于在本地资源字典中覆盖SystemColors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!