Getting TreeViewItem for the selected item in a TreeView

后端 未结 4 1338
遥遥无期
遥遥无期 2020-12-31 05:32

I have a TreeView that is bound to a dataset which is having parent child relation. How i will get seleted TreeViewItem from the TreeView? Please help me. My code is below.

4条回答
  •  别那么骄傲
    2020-12-31 06:05

    This works for trees whose data source is bound via a HierarchicalDataTemplate

    Handle TreeViewItem.Selected

    
    

    And set the TreeViewItem as the Tag.

    private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
    {
        mTreeView.Tag = e.OriginalSource;
    }
    

    Which you can retrieve later

    TreeViewItem tvi = mTreeView.Tag as TreeViewItem;
    

提交回复
热议问题