tags:

views:

680

answers:

2

Hi,

I am new in WPF world. I have treeView that contains

Treeview

Mandate -->

   Portfolio
           ---->portfolio1

Benchmarks

   Category
          ------>Name1  etc..

I would like to know how to get the parent node value in WPF ? For example, If I select the portfolio1 how to get the parent-parent value. In this case Mandate.

In short, I would like to know if user click on Name1 --> I should get Benchmarks and if user click on portfolio1 then I should get Portfolio.

Your guidance is highly appreciated.

Thanks, Regards,

+2  A: 

You can use VisualTreeHelper.GetParent for Name1 and then its parent.

Or you can do it iteratively till you get an object of type TreeViewItem, this way you wont have to update your code when you change the xaml.

If you do it the 2nd way, it would look something like this:

DependencyObject parent = VisualTreeHelper.GetParent(Name1);
while(!(parent is TreeViewItem))
    parent = VisualTreeHelper.GetParent(parent);
TreeViewItem item = (TreeViewItem) parent;
Bubblewrap
A: 

Bubblewrap,

Thank for your reply. Can you please give an example ?

Sure, added an example in my post.
Bubblewrap