Getting the value of the Selected Child Nodes on c

2019-08-12 15:02发布

I have a TreeView in WPF Which contains One Main Node and 5 Child Node.As soon as Main Node is expanded we get child Nodes.Now on Expanding child node we get some Values.This is the representation of my treeView In WPF.In this one i want to get the Value of one of the 5 Child Node that has been expanded.

Here is the code that i am trying ..

void getTreeView()
{

    TreeViewItem treeItem = null;
    treeItem = new TreeViewItem();
    treeItem.Header = "Name";
    treeItem.MouseLeftButtonUp += treeItem_MouseLeftButtonUp;
}

void treeItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
   TreeViewItem item = sender as TreeViewItem;
   foreach(TreeViewItem child in item.Items) { 

       string childNode=child.Header as string;


 }
}

But here in childNode i am getting the value of the all 5 child node whereas i need to that of the selected ones.

Please help me

标签: c# wpf treeview
1条回答
2楼-- · 2019-08-12 15:47

If you want to get only selected node, check for IsSelected property of TreeViewItem like this:

foreach(TreeViewItem child in item.Items) 
{ 
   if(child.IsSelected)
   {
      string childNode= child.Header.ToString();
   }
}
查看更多
登录 后发表回答