C# 将对象轻松绑定到 Treeview 节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1880930/
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
Easy object binding to Treeview Node
提问by Kai
How can I bind an object to a treeview (Winforms) node in C#?
如何将对象绑定到 C# 中的树视图(Winforms)节点?
I thought of something like ExNode : Windows.Forms.Node that can take an object as member besides the treenode name... however I am not sure that is the right approach.
我想到了 ExNode 之类的东西:Windows.Forms.Node 可以将对象作为除树节点名称之外的成员......但是我不确定这是正确的方法。
采纳答案by BillW
imho you have several strategies :
恕我直言,您有几种策略:
stick an object of any type in the Tag property of any Node : downside : you'll have to cast it back to its 'native form' when you retrieve it to use it : if that "native form" is anything but type 'Object.
sub-class TreeNode, and add a public fields, Public Properties, or whatever, for your objects ... or even List ... ... or whatever you need to associate with the Node.
assuming your objects are of the same type, you could create a dictionary of type : Dictionary <TreeNode, myObjectType>, instantiate it, and, as needed, store a TreeNode and its associated Object(s) that way as a Key/Value Pair.
在任何节点的 Tag 属性中粘贴任何类型的对象:缺点:当您检索它以使用它时,您必须将其转换回其“本机形式”:如果该“本机形式”不是类型“对象” .
子类 TreeNode,并为您的对象添加公共字段、公共属性或其他任何内容……甚至列表……或任何您需要与节点关联的内容。
假设您的对象属于相同类型,您可以创建一个类型为的字典:Dictionary <TreeNode, myObjectType>,将其实例化,并根据需要将 TreeNode 及其关联对象存储为键/值对.
Strategies #1, and #3 have the advantage that you can store an associated object ONLY as needed Strategy #2 : is more suited to the case where you anticipate every TreeNode is going to have an associated object(s).
策略 #1 和 #3 的优点是您可以仅根据需要存储关联对象 策略 #2:更适合您预计每个 TreeNode 都将具有关联对象的情况。
Of course with stragies #1 and #3, you will need to test at run-time for the presence or absence of an object associated with a particular Node.
当然,对于策略 #1 和 #3,您需要在运行时测试与特定节点关联的对象是否存在。
Strategy #1's an easy test : if the Tag propety of the Node is Null : you know there's no object : if not null ... and there may be more than one type of object stored in the Tag field ... then you'll have to pull out the Tag object, and make sure it's the right type as in : (the example that follows assumes a public class, "Class1," has been assigned to tag of the first node in the TreeView :
策略#1 是一个简单的测试:如果节点的标签属性为空:你知道没有对象:如果不是空......并且标签字段中可能存储了不止一种类型的对象......那么你'必须拉出 Tag 对象,并确保它的类型正确,如下所示:(以下示例假定公共类“Class1”已分配给 TreeView 中第一个节点的标签:
TreeNode thisNode = theTreeView.Nodes[0];
if (((thisNode.Tag != null) && (thisNode.Tag is Class1))) ... handle the object ...
Strategy #3 is a little easier since you can just evaluate if the Dictionary<Node, myObject>.Contains the Node as a Key.
策略 #3 稍微容易一些,因为您可以只评估 Dictionary<Node, myObject>.Contains 节点是否作为键。
回答by cornergraf
Are you looking for something like the Tag property on TreeNodes? It can hold any object.
您是否正在寻找 TreeNodes 上的 Tag 属性之类的东西?它可以容纳任何物体。
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx
回答by JustinStolle
This MSDN articlehas some good information, for example:
这篇 MSDN 文章有一些很好的信息,例如:
class myTreeNode : TreeNode
{
public string FilePath;
public myTreeNode(string fp)
{
FilePath = fp;
this.Text = fp.Substring(fp.LastIndexOf("\"));
}
}
回答by B H
You might look into the TreeListView. It isn't perfect, but it works very well and makes the process of displaying objects in a tree view much easier than any other way I have found.
您可能会查看TreeListView。它并不完美,但它工作得很好,并且使在树视图中显示对象的过程比我发现的任何其他方式都容易得多。