0

有没有办法TreeView保持Objects而不是简单stringListBox一个函数怎么类似?

或者是否有一个与 a 明显相似的控件TreeView能够持有 a Object

4

3 回答 3

3

如果需要在 TreeNode 中保存对象,可以使用 Tag 属性。

TreeNode node;
node.Tag = myObject;

这将包含一个您可以随时检索的对象。

于 2012-10-20T23:07:54.963 回答
1

您可以创建TreeNode,将其文本设置为您想要的任何内容,将对象分配给Tag属性并将其添加到TreeView

于 2012-10-20T23:07:46.337 回答
1

您还可以创建一个带有属性的 TreeNode 子类来保存您的对象。这是一个通用版本:

public class MyTreeNode<T>:TreeNode {
    private T data;

    public T Data {
        get {
            return data;
        }
        set {
            data = value;
            Text = data.ToString();
        }
    }
}

In this case I'm also setting the TreeNode's text when the data value is set, though more code would be required if you were changing the stored object's state and wanted the TreeNode's Text to reflect that.

于 2012-10-20T23:24:48.663 回答