Make highlight brush the same as control brush without specifying colour
Asked Answered
G

2

8

I want to ensure that a selected ListViewItem's non-focused background is the same as the focused background. I know that the common way of doing this is as follows:

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

However, the problem is that I don't want to specify a colour, I merely want the Brush returned by the static resource whose key is ControlBrushKey to be the same as the one for HighlightBrushKey.

Gober answered 20/9, 2010 at 17:2 Comment(0)
G
13

The answer is this:

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
           Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
Gober answered 21/9, 2010 at 6:21 Comment(0)
D
0

Try this... I know it works to set two properties to match, not sure if it will work in your context, but it's worth a shot:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                 Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                 Color="{DynamicResourse SystemColors.HighlightBrushKey.Color}"/>

I tested this using a TextBox as a playground. I'm not sure of your exact application, but here was my test markup:

<TextBox>
    <TextBox.Background>
        <SolidColorBrush  x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                Color="Blue"></SolidColorBrush>
    </TextBox.Background>
    <TextBox.Foreground>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
                Color="{DynamicResource SystemColors.HighlightBrushKey.Color}" />
    </TextBox.Foreground>
</TextBox>

This just set the background to blue, and the foreground to the background, which was the expected result.

Dumanian answered 20/9, 2010 at 17:26 Comment(1)
It doesn't work. Try the following and you'll see what I mean. Select an item from the ListView, then click on the TextBox. <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ListView> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource SystemColors.HighlightBruGober

© 2022 - 2024 — McMap. All rights reserved.