覆盖本地资源字典中的 SystemColors [英] Overriding SystemColors in local Resources dictionary

查看:26
本文介绍了覆盖本地资源字典中的 SystemColors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏指示 WPF ListBox 中选择的视觉提示.

知道我做错了什么吗?如何删除显示在所选项目和悬停项目上的蓝色?

解决方案

肯定不行.由于 ListBox.Resources 将为 ListBox 而不是为 ListBoxItem 设置资源.所以这里有一个解决方案:

<Style TargetType="{x:Type ListBoxItem}"><Setter 属性="FocusVisualStyle" 值="{x:Null}"/><风格.资源><SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/><SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="透明"/></Style.Resources></风格>

将此样式附加到 windows.resource 中,如:

<Window.Resources><Style TargetType="{x:Type ListBoxItem}"><Setter 属性="FocusVisualStyle" 值="{x:Null}"/><风格.资源><SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/><SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="透明"/></Style.Resources></风格></Window.Resources>

而且你可以将更多的 SolidColorBrush 放入 Style.Resources 中,就像你在代码中所做的那样.

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.

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?

解决方案

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>

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>

And defiantly you can put more SolidColorBrush into Style.Resources like what you did in your code.

这篇关于覆盖本地资源字典中的 SystemColors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆