11
votes

I understand that programming in C# with WPF is different from traditional C# procedures so most of the online material do not state what I need.

I have a TreeView control in my WPF Window and I have parent nodes and child nodes in it. I would like to store them in a List of type Node (id, name, parent).

enter image description here

I got the name of the selected item/node using this:

private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
    TreeViewItem item = treeView.SelectedItem as TreeViewItem;
    nameTxt.Text = item.Header.ToString();
}

And I tried getting the Parent of the child node immediately before it using this:

TreeViewItem item = treeView.SelectedItem as TreeViewItem;
nameTxt.Text = item.Parent.ToString();

However, this returns the Root Parent (A) instead of the child's parent (which is 2).

What changes should I make to get the child's immediate parent instead of the root parent? :)

EDIT: Here's the XAML

<TreeView Name="treeView" HorizontalAlignment="Left" Height="564" Margin="10,68,0,0" VerticalAlignment="Top" Width="363">
     <TreeViewItem TreeViewItem.Selected="TreeViewItem_OnItemSelected"  Header="A" IsExpanded="True" Height="554" FontSize="18">
                <TreeViewItem Header="1" />
                <TreeViewItem Header="2" />
     </TreeViewItem>
</TreeView>
1
That's unusual it's the parent of the node that is returned by the Parent property.Could you perhaps show an extended version of the code you provided,maybe the problem is somewhere else - Christo S. Christov
What was the SelectedItem when item.Parent returns A? - Sriram Sakthivel
@Chris this is the complete code, mate. I'm just printing out the selected item and the parent to a textbox. It just keeps on mapping to the root node which is A. However the selected item shows the currently selected item, it's just that the parent keeps on getting mapped to the root node instead of the parent immediately before - MWUser
@SriramSakthivel refer comment above, good sir! :) - MWUser
Your Xaml doesn't reflect the UI you shown in image, you have only two nodes whose parent is A, so you get A. I can't reproduce the problem either. Post full code to reproduces the problem to get help. Btw I recommend you to use data binding instead refer this. - Sriram Sakthivel

1 Answers

25
votes

Created a small example to demonstrate your problem

In MainWindow.xaml

 <TreeView Name="tree">
    <TreeView>
        <TreeViewItem Header="North America" Selected="TreeViewItem_OnItemSelected">
            <TreeViewItem Header="USA">
                <TreeViewItem Header="New York"/>
                <TreeViewItem Header="Las Vegas"/>
                <TreeViewItem Header="Washington"/>
            </TreeViewItem>
            <TreeViewItem Header="Canada">
                <TreeViewItem Header="Toronto"/>
                <TreeViewItem Header="Quebec"/>
                <TreeViewItem Header="Montreal"/>
            </TreeViewItem>
            <TreeViewItem Header="Mexico"></TreeViewItem>
        </TreeViewItem>
    </TreeView>
</TreeView>

in Code Behind i.e MainWindow.xaml.cs

 private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
    {
        TreeViewItem item = e.OriginalSource as TreeViewItem;
        if (item != null)
        {
            ItemsControl parent = GetSelectedTreeViewItemParent(item);

            TreeViewItem treeitem = parent as TreeViewItem;
            string MyValue= treeitem .Header.ToString();//Gets you the immediate parent
        }
    }
    public ItemsControl GetSelectedTreeViewItemParent(TreeViewItem item)
    {
        DependencyObject parent = VisualTreeHelper.GetParent(item);
        while (!(parent is TreeViewItem || parent is TreeView))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }

        return parent as ItemsControl;
    }

And its done.