在 WPF C# TreeView 中获取子节点的父节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29005119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:00:31  来源:igfitidea点击:

Get the Parent node of a Child in WPF C# TreeView

c#.netwpftreeview

提问by MWUser

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.

我知道使用 WPF 在 C# 中编程与传统的 C# 程序不同,因此大多数在线材料都没有说明我需要什么。

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).

我的 WPF 窗口中有一个 TreeView 控件,其中有父节点和子节点。我想将它们存储在类型节点(ID、名称、父级)的列表中。

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).

但是,这将返回根父级 (A) 而不是子级的父级(即 2)。

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

我应该做哪些更改才能获得孩子的直接父母而不是根父母?:)

EDIT: Here's the XAML

编辑:这是 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>

回答by Rohit

Created a small example to demonstrate your problem

创建了一个小示例来演示您的问题

In MainWindow.xaml

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

在后面的代码中,即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.

它完成了。