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

喜你入骨 提交于 2019-12-01 02:10:47

问题


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


回答1:


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.



回答2:


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>


来源:https://stackoverflow.com/questions/876759/how-do-i-highlight-a-treeview-selected-item-with-some-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!