1
votes

I am migrating my WPF app from .NET Framework to .NET Core 3.0.

Previously I've used the following 'hack' to override the selected unfocused background color for TreeViewItem:

<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
        </Style.Resources>
    </Style>
</TreeView.ItemContainerStyle>

But it doesn't work in .NET Core 3.0: the unfocused selected items still have a light gray background.

The default template in MSDN is using {StaticResource SelectedUnfocusedColor} for this color so I tried overriding it by putting a desired <Color> in the Resources section of the TreeView - it didn't help.

I've also tried creating a <Trigger> in the Style.Triggers for TreeViewItem Style, setting background color to {x:Static SystemColors.HighlightColor} when IsSelected is True, but it didn't help either.

I'm out of ideas and Google doesn't offer much help (the only other idea I didn't try was to completely retemplate the TreeViewItem which seems a bit of an overkill considering the size of the default template.

1
Which version of .NET Core 3.0 are you using?Pavel Anikhouski
The latest one, preview3.mephisto123
@mephisto123: Did you try defining a SolidColorBrush resource with an x:Key of SystemColors.InactiveSelectionHighlightBrushKey?mm8
It works! Thanks a ton! Please post it as an answer so I can mark it as a correct one :)mephisto123

1 Answers

1
votes

The default template uses SystemColors.InactiveSelectionHighlightBrushKey so you should "override" this brush:

<Style TargetType="TreeViewItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
    </Style.Resources>
</Style>