C# 在 WPF 树视图中的节点下添加节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11216510/
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-08-09 17:00:24  来源:igfitidea点击:

adding nodes under node in WPF treeview

c#wpf

提问by amit kohan

In Visual C++/MFC we used to add a node to a tree and then by referencing the node we could add children under parent node. However, in WPF there is no such a thing as I see. I'm confused how I can add child/children to a node?

在 Visual C++/MFC 中,我们曾经将一个节点添加到树中,然后通过引用该节点,我们可以在父节点下添加子节点。但是,在 WPF 中没有我看到的这样的东西。我很困惑如何将子/子节点添加到节点?

any help will be appreciated.

任何帮助将不胜感激。

it seems 2 people know MVVM already!

似乎有两个人已经知道 MVVM 了!

Solution is given by Tim below.

下面的 Tim 给出了解决方案。

采纳答案by EtherDragon

A quick google search "wpf treeview" found several great articles on how to correctly use treeviews in WPF.

快速谷歌搜索“wpf treeview”找到了几篇关于如何在 WPF 中正确使用树视图的好文章。

Example 1: http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

示例 1:http: //www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode

Example 2: http://www.c-sharpcorner.com/uploadfile/mahesh/treeview-in-wpf/

示例 2:http: //www.c-sharpcorner.com/uploadfile/mahesh/treeview-in-wpf/

That should get you started - update your question when you have tried the MVVM approach and have more specific questions.

这应该让你开始 - 当你尝试过 MVVM 方法并有更具体的问题时更新你的问题。

回答by Tim

Since the OP said my comment was really what he considered an answer, I figured I'd go ahead and turn it into an answer.

由于 OP 说我的评论确实是他认为的答案,因此我想我会继续将其变成答案。

What is described in the question is exactly how you could do it in WPF. For instance:

问题中描述的正是您如何在 WPF 中做到这一点。例如:

var item = new TreeViewItem(); 
myTreeView.Items.Add(item); 
var subItem1 = new TreeViewItem(); 
var subItem2 = new TreeViewItem(); 
item.Items.Add(subItem1); 
item.Items.Add(subItem2);

That'll add a bunch of blank items.

这将添加一堆空白项目。

You can use the Header property of each TreeViewItem to control what is displayed and use the Tag property to hold data, if you want to go that route.

您可以使用每个 TreeViewItem 的 Header 属性来控制显示的内容,并使用 Tag 属性来保存数据,如果您想走那条路的话。

It would likely be preferable, however, to go the binding route and use HierarchicalDataTemplates to control the look. That way you're not manually creating these fake containers (the TreeViewItems) for your data.

然而,最好走绑定路线并使用 HierarchicalDataTemplates 来控制外观。这样您就不会为您的数据手动创建这些假容器(TreeViewItems)。

I'd suggest reading up on HierarchicalDataTemplates, as that'll give you a decent overview of how the process should work with bindings. Also just read up on MVVM in general.

我建议阅读HierarchicalDataTemplates,因为这会给你一个关于流程应该如何与绑定一起工作的体面概述。也只是一般阅读 MVVM。

回答by Gintama

Create your model like this

像这样创建你的模型

public class WrappedNode
{
    public string Name { get; set; }
    public ObservableCollection<WrappedNode> Nodes { get; set; }

    public WrappedNode()
    {
        Nodes = new ObservableCollection<WrappedNode>();
    }        
}

Node list you want to bind to treeview

要绑定到树视图的节点列表

private ObservableCollection<WrappedNode> _nodeList;
public ObservableCollection<WrappedNode> NodeList
{
    get { return _nodeList; }
    set
    {
        _nodeList = value;
        RaisePropertyChanged(() => NodeList);
    }
}

And in xaml:

在 xaml 中:

    <TreeView Grid.Row="1"
              ItemsSource="{Binding NodeList}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type scnvm:WrappedNode}" ItemsSource="{Binding Nodes}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

If you want a node have children, just add child node to Nodes property of that node

如果你想让一个节点有子节点,只需将子节点添加到该节点的 Nodes 属性

回答by Zohaib Ahmed

To add an item as parent:

要将项目添加为父项:

var item = new TreeViewItem();
item.Header = "First Element";
tree.Items.Add(item); //tree is your treeview

To add an element as a child of a specific element :

要将元素添加为特定元素的子元素:

var subItem = new TreeViewItem();
subItem.Header = "Subitem";
var parent = tree.SelectedItem as TreeViewItem;  // Checking for selected element
parent.Items.Add(subItem);