user9969
user9969

Reputation: 16040

Treeview Getting parentNode xaml wpf

I have a treeview in wpf and I load it ok is all done in zaml . I have a problem and this is mainly because I am new to zaml.

If i have this structure

    England
      London
      Manchester
      Liverpool
      etc... 

and I select london i need to display "England-London" . I dont seem to get how to retrieve the parent of the selected child.

Can you help?

Thanks

Upvotes: 0

Views: 2667

Answers (1)

Jojo Sardez
Jojo Sardez

Reputation: 8558

Try to add SelectedItemChanged event to your TreeView and use this code:

private void yourTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (sender is TreeView && ((TreeViewItem)((TreeView)sender).SelectedItem).Parent != null)
        {
            TreeViewItem parent = (TreeViewItem)((TreeViewItem)((TreeView)sender).SelectedItem).Parent;
            //do your stuff here
        }
    }

Upvotes: 1

Related Questions