我试图隐藏一些视觉提示,这些提示指示在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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/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
发布评论