OnExpanded event for any item in a treeview

后端 未结 1 782
[愿得一人]
[愿得一人] 2021-01-03 23:20

I\'d like to get an event for any expansion of a treeviewitem in my treeview.

The reason for this, a bit unrelated to the original question: I am creating a tree th

相关标签:
1条回答
  • 2021-01-03 23:44

    You can use the TreeViewItem.Expanded event as an attached event :

    <TreeView TreeViewItem.Expanded="TreeViewItem_Expanded"
              ItemsSource="{Binding}">
        ...
    </TreeView/>
    

    In code-behind, you can identify the TreeViewItem that initiated the event using the OriginalSource property :

        private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = e.OriginalSource as TreeViewItem;
            if (tvi != null)
            {
                MessageBox.Show(string.Format("TreeNode '{0}' was expanded", tvi.Header));
            }
        }
    
    0 讨论(0)
提交回复
热议问题