简体   繁体   中英

How to override SelectedUnfocusedColor for TreeViewItem

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM