[英]How can I create an index accessor for a List<T> Property I have in a custom class?
Here's the example: 这是示例:
/// <summary>
/// Collection of SednaTreeViewItems used to populate the SednaTreeView.
/// </summary>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<SednaTreeViewItem> TreeNodes
{
get { return treeNodes; }
set
{
ultraTree.Nodes.Clear();
treeNodes = value;
foreach (var item in treeNodes)
{
UltraTreeNode node = new UltraTreeNode(item.ValueMember, item.DisplayMember);
ultraTree.Nodes.Add(node);
}
if (treeNodes.Count() > 0)
ultraTree.ActiveNode = ultraTree.Nodes[0];
}
}
/// <summary>
/// SednaTreeViewItem that is currently selected in the SednaTreeView.
/// </summary>
public SednaTreeViewItem SelectedItem
{
get
{
if (ultraTree.SelectedNodes != null)
{
var node = ultraTree.SelectedNodes[0];
SednaTreeViewItem item = new SednaTreeViewItem(node.Key, node.Text);
return item;
}
else
return null;
}
set
{
ultraTree.ActiveNode = ultraTree.Nodes[value.ValueMember];
}
}
I'd like to use my control in this way. 我想以这种方式使用我的控件。 For example, someone wants to set the selected tree view node during runtime, I'd like something like:
例如,某人想要在运行时设置选定的树视图节点,我想要类似以下内容:
treeViewInvestors.SelectedItem = treeViewInvestor.TreeNodes[userIdKey];
Where userIdKey is a unique key that's already in place behind the scenes. 其中userIdKey是在幕后已经存在的唯一键。
Basically, is there a way to create an index "[]" accessor for my property? 基本上,有没有一种方法可以为我的媒体资源创建索引“ []”访问器?
If the exposed node collection property is a List (that already has an indexer of type int), and you need an indexer of another type, then you could expose the treenode collection as a subclass of list that has the other type of indexer: 如果暴露的节点集合属性是List(已经具有int类型的索引器),并且您需要其他类型的索引器,则可以将treenode集合公开为具有另一种索引器类型的list的子类:
public class NodeList : List<Node>
{
public TNode this[Key key]
{
get { return nodes.Where(n => n.Key == key).SingleOrDefault(); }
}
}
Then you can access by both kinds of index 然后就可以通过两种索引访问
var nodeByKey = treeViewInvestor.TreeNodes[key];
var nodeByIndex = treeViewInvestor.TreeNodes[index].
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.