How do I highlight a treeview selected item with some color?

前端 未结 2 997
故里飘歌
故里飘歌 2021-01-12 23:49

I have a treeview in WPF. I want a different color when i select the treeviewitem.

相关标签:
2条回答
  • 2021-01-13 00:04

    Try following code. It should work.

    <Style TargetType="{x:Type TreeViewItem}">
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Yellow"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    
    0 讨论(0)
  • 2021-01-13 00:16

    Simple Trigger in TreeView.ItemContainerStyle can't help for default TreeView template.

    For standard template highlighting is done via background changing for specific element inside TreeView template. This specific element is not accessible for programmer without TreeView template changing. By default resource is used to set background on this element for highlighting.

    So there are few ways:

    1. simple (but side effects possible): redefine resource with key {x:Static SystemColors.HighlightBrushKey} for TreeView or ItemsPanel template;
    2. Redefine complete Template for TreeView.
    0 讨论(0)
提交回复
热议问题