在View中使用自定义TreeView类

时间:2018-02-02 21:30:20

标签: c# winforms treeview

我有一个自定义TreeView类,如下所示:

using System.Windows.Forms;
namespace My.Namespace
{
    public partial class SimpleTree : System.Windows.Forms.TreeView
    {
        private TreeNode topNode; ////////// 
        private TreeNode lowerNode1;
        private TreeNode lowerNode2;
        private TreeNode lowerNode3;

        public SimpleTree()
        {
            InitializeComponent();

            BuildTree();

            TreeViewEventArgs eventArgs = new TreeViewEventArgs(topNode, TreeViewAction.Unknown);

            this.ExpandAll(); // must be after adding the nodes

        }

        private void BuildTree()
        {
            this.Name = "Simple Tree";

            topNode = new TreeNode("Top Node");
            topNode.Name = "Top Node"
            this.Nodes.Add(topNode);

            lowerNode1 = new TreeNode("Lower Node 1");
            lowerNode1.Name = "Lower Node 1";
            topNode.Nodes.Add(lowerNode1);

            lowerNode2 = new TreeNode("Lower Node 2");
            lowerNode2.Name = "Lower Node 2";
            topNode.Nodes.Add(lowerNode2);

            lowerNode3 = new TreeNode("Lower Node 3");
            lowerNode3.Name = "Lower Node 3";
            topNode.Nodes.Add(lowerNode3);
        }
        // This is a simplified example, much more functionality here in real code
    }
}

我在ProjectView.cs中包含了这个SimpleTree(它有很多组件) 在ProjectView.Designer.cs中,有:

private SimpleTreeView trvSimple;
this.trvSimple = new My.Namespace.SimpleTreeView();

this.trvSimple.BackColor = System.Drawing.Color.White;
this.trvSimple.Location = new System.Drawing.Point(3, 65);
this.trvSimple.Name = "trvSimple";
this.trvSimple.Scrollable = false;
this.trvSimple.Size = new System.Drawing.Size(128, 109);
this.trvSimple.TabIndex = 1;
this.trvSimple.Text = "trvSimple";
this.trvSimple.BeforeCollapse += new System.Windows.Forms.TreeViewCancelEventHandler(this.trvSimple_BeforeCollapse);
this.trvSimple.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.trvSimple_BeforeSelect);

这一切都有效。树看起来像:

  

顶级节点
   - 下节点1
   - 下节点2
   - 下节点3

但是,当我在ProjectView中更改其他内容时,Visual Editor会将此代码添加到ProjectView.Designer.cs

System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Lower Node 1");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Lower Node 2");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Lower Node 3");
System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Top Node", new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode2,
treeNode3});

所以现在它看起来像这样:

  

顶级节点
   - 下节点1
   - 下节点2
   - 下节点3
  顶级节点
   - 下节点1
   - 下节点2
   - 下节点3

如何阻止添加此代码?

1 个答案:

答案 0 :(得分:1)

您可能不应该从自定义控件执行此操作。在表单的构造函数中运行时填充TreeView控件会更简单。

如果您仍希望通过自定义控件执行此操作,则可以尝试仅在not design mode时添加节点:

public SimpleTree() {
  if (LicenseManager.UsageMode != LicenseUsageMode.Designtime) {
    BuildTree();
  }
  this.ExpandAll();
}

另一种可能性是覆盖节点集合并强制它在设计时不序列化数据:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new TreeNodeCollection Nodes {
  get {
    return base.Nodes;
  }
}

这会使设计时添加或编辑任何节点毫无意义,因为它不会保存任何东西。