wpfhighlightsystemcolors

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


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?


Solution

  • 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>