将WPF列表框的非活动突出显示颜色设置为活动突出显示颜色 [英] Setting the Inactive Highlight Colour of a WPF ListBox to the Active Highlight Colour

查看:133
本文介绍了将WPF列表框的非活动突出显示颜色设置为活动突出显示颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个列表框,无论列表框是否具有焦点,突出显示的项目看起来都一样.

I'm trying to make a ListBox where highlighted items look the same regardless of if the ListBox has focus or not.

基本上,我想将SystemColors.ControlBrushKey颜色属性设置为与SystemColors.HighlightBrushKey颜色相同.

Essentially I want to set the SystemColors.ControlBrushKey color property to be the same as the SystemColors.HighlightBrushKey color.

我认为我可以使用以下内容:

I thought I could use the following:

<ListBox>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                         Color="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
    </ListBox.Resources>
</ListBox>

但这实际上会引发以下错误:

But this actually throws the following error:

System.Windows.Markup.XamlParseException:设置属性"System.Windows.Media.SolidColorBrush.Color"引发了异常. ---> System.ArgumentException:'#FF3399FF'不是属性'Color'的有效值

System.Windows.Markup.XamlParseException: Set property 'System.Windows.Media.SolidColorBrush.Color' threw an exception. ---> System.ArgumentException: '#FF3399FF' is not a valid value for property 'Color'

如果我设置了Color="#FF3399FF",它将正常工作.

If I set Color="#FF3399FF" it works fine.

我在做什么错了?

推荐答案

感谢Nicholas W指出正确的方向-这是ListBox的完整代码:

Credit to Nicholas W for pointing me in the right direction - here's the full code for the ListBox:

<ListBox Width="200" Height="200">
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                         Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
    </ListBox.Resources>
    <ListBox.ItemContainerStyle>
        <Style>
            <Style.Triggers>
                <Trigger Property="Selector.IsSelected" Value="True">
                    <Setter Property="TextElement.Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem IsSelected="True">Item A</ListBoxItem>
    <ListBoxItem>Item B</ListBoxItem>
    <ListBoxItem>Item C</ListBoxItem>
</ListBox>

这篇关于将WPF列表框的非活动突出显示颜色设置为活动突出显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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