在本地资源字典中覆盖SystemColors

编程入门 行业动态 更新时间:2024-10-24 08:27:09
本文介绍了在本地资源字典中覆盖SystemColors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图隐藏一些视觉提示,这些提示指示在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

本文发布于:2023-11-12 03:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580414.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   资源   SystemColors

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!