Access WPF Treeview selected index through a TreeViewItem object

后端 未结 1 1715
误落风尘
误落风尘 2021-01-17 05:02

I have a treeview with checkboxes for each item using a DataTemplate.




        
相关标签:
1条回答
  • 2021-01-17 05:16

    The ItemsControl you are fetching might be the StackPanel, or the Grid. You should be able to access the Checkbox through the event sender, and navigate up to the TreeViewItem and TreeView and use IndexOf.

     private void CheckBox_Click(object sender, RoutedEventArgs e)
     {
            CheckBox cb = (CheckBox)sender;
            StackPanel sp = (StackPanel)cb.Parent;
            Grid g = (Grid)sp.Parent;
            ContentPresenter cp = (ContentPresenter)VisualTreeHelper.GetParent(g);
            IList l = (IList)myTreeView.ItemsSource;
            object o = cp.Content;
            MessageBox.Show(l.IndexOf(o).ToString());
     }
    
    0 讨论(0)
提交回复
热议问题