将自定义类的集合绑定到TreeView [英] Binding collection of custom classes to TreeView
问题描述
我目前有两个自定义类,一个保存在a Collection,以及在第一类内部的Collection中保存的
Collection< Device> _devices = new Collection< Device>();
class Device
{
public ulong DeviceID {get;私人集}
private List< Capability> _capabilities = new List< Capability>();
public string FriendlyName {get;组; }
public Capability AddCapability(能力能力)
{
_capabilities.Add(capability);
返回能力;
}
public List< Capability> GetCapabilities()
{
//不安全
return _capabilities;
}
}
抽象类能力
{
public uint CapabilityIndex {get;私人集}
public string FriendlyName {get;组; }
}
我试图获得一个 TreeView
以显示已展开时列出功能的设备集合。
我考虑的选项包括:
- 从我要显示的数据中循环并创建TreeNode对象(但我不知道在选择时如何最好地引用回原始对象)
- 从TreeNode类继承,但这感觉很丑陋,因为我在多个地方存储相同的数据(大多数时候,我不需要他们作为TreeNodes,所以感觉像浪费资源)
有任何建议吗?
>当处理一个TreeView时,我通常最终生成一个包装器类继承自TreeNode并且保存我作为节点显示的实际对象。这样我可以绑定到TreeView,获得我需要的父级,我的业务类实际上不必子类化UI组件。它在处理TreeView事件时很好地工作,因为你可以只检查节点的类型,不同的类型节点可以不同的方式处理 - 显示不同的UI或任何东西。
private class FooNode:TreeNode
{
public FooNode(Foo foo)
{
this.Text = foo.ToString(); //或FriendlyName
this.Foo = foo;
this.Foo.Bars.ForEach(x => this.Nodes.Add(new BarNode(x)));
}
public Foo Foo
{
get;
private set;
}
}
private class BarNode:TreeNode
{
public BarNode(Bar bar)
{
this.Text = bar.ToString(); //或FriendlyName
this.Bar = bar;
}
public Bar Bar
{
get;
private set;
}
}
这个例子可能正是你的第二个选项。在这种情况下,我投票第二个选项!
I'm trying to determine the best way to accomplish binding a Collection of a custom class to a TreeView.
I currently have two custom classes, one held in a Collection, and one held in a Collection inside the first class:
Collection<Device> _devices = new Collection<Device>();
class Device
{
public ulong DeviceID { get; private set; }
private List<Capability> _capabilities = new List<Capability>();
public string FriendlyName{ get; set; }
public Capability AddCapability(Capability capability)
{
_capabilities.Add(capability);
return capability;
}
public List<Capability> GetCapabilities()
{
// not safe yet
return _capabilities;
}
}
abstract class Capability
{
public uint CapabilityIndex { get; private set; }
public string FriendlyName{ get; set; }
}
I'm trying to get a TreeView
to display the collection of devices which when expanded lists the capabilities.
Options I've considered include:
- looping through and creating TreeNode objects from the data I want to display (but I'm not sure how is best to refer back to the original object when selected)
- inheriting from the TreeNode class but this feels ugly as I'm storing the same data in multiple places (and most of the time I don't need them as TreeNodes so it feels like a waste of resources)
Any suggestions?
When dealing with a TreeView, I usually end up generating a wrapper class that inherits from TreeNode and which holds the actual object that I'm displaying as the node. That way I can bind to the TreeView, get the heirarchy I need, and my business classes don't actually have to subclass a UI component. It works nicely when handling the TreeView events as well, because you can just inspect the node for its type and different typed nodes can be handled differently - display a different UI or whatever.
private class FooNode : TreeNode
{
public FooNode(Foo foo)
{
this.Text = foo.ToString(); //Or FriendlyName
this.Foo = foo;
this.Foo.Bars.ForEach(x => this.Nodes.Add(new BarNode(x)));
}
public Foo Foo
{
get;
private set;
}
}
private class BarNode : TreeNode
{
public BarNode(Bar bar)
{
this.Text = bar.ToString(); //Or FriendlyName
this.Bar = bar;
}
public Bar Bar
{
get;
private set;
}
}
This example may be exactly what you mean by your second option. In that case, I vote for the second option!
这篇关于将自定义类的集合绑定到TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!