Setting the Inactive Highlight Colour of a WPF ListBox to the Active Highlight Colour

浪尽此生 提交于 2019-12-13 14:26:53

问题


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

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: Set property 'System.Windows.Media.SolidColorBrush.Color' threw an exception. ---> System.ArgumentException: '#FF3399FF' is not a valid value for property 'Color'

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

What am I doing wrong?


回答1:


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>


来源:https://stackoverflow.com/questions/10139740/setting-the-inactive-highlight-colour-of-a-wpf-listbox-to-the-active-highlight-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!