如何覆盖TreeNodeCollection.Add(TreeNode)?
我有这个:
public abstract class DRT_Tvw_Abstract : TreeView
{
TableCountMetrics metrics;
public TableCountMetrics Metrics { get => metrics; set => metrics = value; }
public class TableCountMetrics
{
int schemaNameCount = 0;
int tableNameCount = 0;
public int SchemaNameCount { get => schemaNameCount; set => schemaNameCount = value; }
public int TableNameCount { get => tableNameCount; set => tableNameCount = value; }
}
public DRT_Tvw_Abstract() : base()
{
}
}
public class DRT_Tvw_TableList_Unfiltered : DRT_Tvw_Abstract
{
public DRT_Tvw_TableList_Unfiltered() : base()
{
}
public void _CreateTreeView(DataTable dataTable)
{
tableListTreeViewNode = new {treeview node from my class that derives from TreeNode}
Nodes.Add(tableListTreeViewNode);
}
我想做的是 Override
Add
方法,这样我就可以递增/递减属于 DRT_Tvw_Abstract
类的一部分的自定义整数属性同时我添加
一个 TreeNode
.我首先尝试从 TreeNodeCollection
类派生,但这似乎是死路一条,因为我无法弄清楚 TreeNodeCollection
类的有效ctor是什么
What I want to do is override
the Add
method so I can increment/decrement custom integer properties that are part of my DRT_Tvw_Abstract
class at the same time I Add
a TreeNode
. I started by trying to derive from the TreeNodeCollection
class, but that seems to be a dead-end because I can't figure out what a valid ctor is for the TreeNodeCollection
class
Nodes
(如 Nodes.Add
中)是一个属性.我如何从中衍生出来,以便覆盖 Add
方法?我首先创建了此特性的替代,但是对于如何从属性替代代码中替代方法( Add
),我一无所知.
Nodes
(as in Nodes.Add
) is a property. How do I derive from it so I can override the Add
method? I started by creating an override for this poprperty, but I have no clue as to how you override a method (Add
) from within property override code.
public new virtual TreeNodeCollection Nodes
{
//override the Add method from within here somehow?
get
{
return base.Nodes;
}
set
{
}
}
重写TreeNodeCollection类的Add(TreeNode)方法的语法是什么?
What is the syntax for overriding the TreeNodeCollection class Add(TreeNode) method?
自然地,您可能会考虑从 Naturally you may think about deriving from 您可能想到的另一种解决方案是从 The other solution that you may think about it is deriving from 示例-扩展方法 创建一个新的代码文件,然后复制以下代码并将其粘贴到该代码文件中: Create a new code file and copy the following code and paste it in the code file: 然后在要使用新的 Then in the class which you are going to use the new 然后像普通方法一样使用它: Then use it like a normal method: Add
方法属于 TreeNodeCollection
派生并定义新方法,并从 TreeView
派生并将其 TreeNodeCollection
替换为新的自定义节点集合.但是不要尝试此解决方案. TreeNodeCollection
没有公共的构造函数.因此,我建议创建
TreeNodeCollection
and defining new methods and derive from TreeView
and replace its TreeNodeCollection
with your new custom node collection. But don't try this solution. TreeNodeCollection
doesn't have a public constructor. So I'd suggest creating Extension Methods. TreeView
派生并将新的 Add
方法添加到派生的 TreeView
.TreeView
and adding new Add
methods to the derived TreeView
. namespace MyExtensions
{
using System.Windows.Forms;
public static class TreeNodeCollectionExtensions
{
//Just an example of a new signature, use the signature that you need instead:
public static TreeNode Add(this TreeNodeCollection nodes, string text, int tag)
{
var node = nodes.Add(text);
node.Tag = tag;
return node;
}
}
}
Add
方法的类中,首先添加using语句:Add
method, first add the using statement:using MyExtensions;
treeView1.Nodes.Add("One", 1);