Setting the Inactive Highlight Colour of a WPF Lis

2019-07-15 16:18发布

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条回答
相关推荐>>
2楼-- · 2019-07-15 16:58

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>
查看更多
登录 后发表回答