How to get TreeViewItem from HierarchicalDataTemplate item?

前端 未结 11 1433
失恋的感觉
失恋的感觉 2020-12-02 11:34

I have a TreeView which uses a HierarchicalDataTemplate to bind its data.

It looks like this:



        
11条回答
  •  生来不讨喜
    2020-12-02 12:02

    I ran into this same problem. I needed to get to the TreeViewItem so that I could have it be selected. I then realized that I could just add a property IsSelected to my ViewModel, which I then bound to the TreeViewItems IsSelectedProperty. This can be achieved with the ItemContainerStyle:

    
                
                    
                
    
    

    Now if I want to have an item in the treeview selected, I just call IsSelected on my ViewModel class directly.

    Hope it helps someone.

提交回复
热议问题