Change WPF treeViewItem Background color when selected but lost focus

前端 未结 2 2159
独厮守ぢ
独厮守ぢ 2021-02-19 04:50

I am trying to change a TreeViewItem background color when it selected but lost focus. I saw some similar questions like: WPF TreeViewItem Background , but I couldn\'t use it...

2条回答
  •  别那么骄傲
    2021-02-19 05:21

    In default template of TreeViewItem, there is MultiDataTrigger which set the color to ControlBrushKey when item is not in focus which you can override in your TreeView resources like this:

    
       
    
    

    then there is no need to override ControlTemplate of TreeViewItem.

    But, however it also set text of treeViewItem to ControlTextBrushKey which by default is black. So, with only ControlBrushKey overriden, you will get it work like this:

    Without Focus:

    enter image description here

    With Focus

    enter image description here

    As you can see brush is over-ridden properly but if you override ControlTextBrushKey and set it to white than treeView Item's won't be visible since controlTextBrush will be set to white.


    However, in case you want exact same look of TreeViewItem like it has with focus, you have to override control template and remove that MultiDataTrigger from the default template.

    Default template will go like this:

        
            
                
                                        
                                    
                                    
                                        
                                    
                                    
                                
                                
                                    
                                        
                                    
                                    
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                        
                                            
                                                
                                            
                                        
                                    
                                    
                                        
                                            
                                                
                                            
                                        
                                    
                                
                            
                        
                    
                
            
            
            
        
    

提交回复
热议问题