Setting font of parent in nodes in WPF Tree View to Bold using code behind

后端 未结 3 818
青春惊慌失措
青春惊慌失措 2021-01-25 19:09

I have made a tree view and added parent and child nodes to it all using code behind, now where I am stuck is to set the font of parent nodes to BOLD while keeping

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-25 19:19

    I can't tell you how to fix that code-behind to do that. What I can do is tell you that you don't need code-behind to do this. That looks like a nightmare.

    Here's a simple example of how to use binding, a style, and a trigger to accomplish what you're talking about. Don't get too overwhelmed by the fact that I'm using an XmlDataProvider here - that's just so that it's a working example that you can paste into Kaxaml and play with. When I say "simple," what I mean is that the whole thing is accomplished by a single binding, a single template, and a single style.

    The key here is that the style in the HierarchicalDataTemplate sets the FontWeight to Bold by default, and then there's a DataTrigger that sets it to Normal if the item has no children. (So you'll notice, as you expand the tree, that the fact that an item is boldfaced tells you that it has children. Which looks kinda nice.)

    If you're binding to something other than an XmlDataSource, you probably have a property on the source that the DataTrigger can check, with a value that tells you whether or not it's a child; just plug that property and value into the style.

    
      
        
          
            
              
                
                
                  
                  
                
                
              
              
                
                
                
              
            
          
        
      
        
        
          
            
              
                
                  
                
              
            
          
        
      
    
    

提交回复
热议问题