简体   繁体   English

如何为列表创建索引访问器 <T> 我在自定义课程中拥有的物业?

[英]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? 基本上,有没有一种方法可以为我的媒体资源创建索引“ []”访问器?

Implement indexer property like: 实现索引器属性,例如:

public SednaTreeViewItem this[int index]//or public T this[int index]
{
   get { return ultraTree[index]; }
   set { ultraTree[index] = value; }
}

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.

相关问题 我可以在不使用自定义设置访问器的情况下监视属性的更改吗? - Can I monitor a property for changes without using a custom set accessor? 如何在自定义列表上调用方法<T>从属性? - How can I call method on custom List<T> from a property? 我可以拥有/使用带有列表的字段/属性吗 <Class<T> &gt; T可以是什么? - Can I have/use a field/property with a List<Class<T>> where T can be anything? 如何将 Class 公共属性设置为类型列表<t> ?</t> - How can I set a Class Public Property as Type List<T>? 我可以创建一个列表<class<t> &gt;? </class<t> - Can I create a List<Class<T>>? 我有两个字符串列表,如何从每个列表索引中创建一个字符串? - I have two Lists of string how can i create a string from each List index with the other one? 如何隐藏列表 class 中的属性 - How can I hide property in list class 我可以使默认集合编辑器和/或自定义UIEditor调用属性的set访问器吗? - Can I make the default collection editor and/or a custom UIEditor envoke the set accessor for a property? 如何创建嵌套类列表,其中每个嵌套 class 的某个属性为真? - How can I create list of nested classes where a certain property of each nested class is true? 在C#中,我可以为自动实现的属性访问器创建委托实例吗? - In C# can I create a delegate instance to an auto-implemented property accessor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM