Create Event Handler for TreeViewItem in WPF

后端 未结 4 1932
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 05:22

Im adding items to TreeView control via ItemsSource property and ItemTemplate property to set the template for TreeViewItem. How can i add an event handler to handle selecti

4条回答
  •  迷失自我
    2021-01-06 05:25

    If you want to capture the SelectedItemChanged event in a TreeView, then you need to set the event handler on the parent node, i.e.,

    XAML

    
                  
            
                
                
                
            
            
                
                
                
            
            
                
                
                
            
        
    
        
    
    

    Code Behind:

    private void OnTreeViewSelectedItemChanged( object sender, RoutedPropertyChangedEventArgs e )
    {
        MyTextBlock.Text = ( (TreeViewItem) ( (TreeView) sender ).SelectedItem ).Header.ToString();
    }
    
        

    提交回复
    热议问题