如何使用winforms以多列模式向TreeViewAdv添加项目

时间:2012-04-09 11:38:30

标签: c# winforms treeview

我正在玩这个名为TreeViewAdv的控件。我已经添加了控件并添加了几列,试图了解它是如何工作的。但直到现在,当我向控件添加项目时,我得到的只是空节点。控件提供的示例根本不清楚,当我试图弄清楚如何将数据添加到它时,它只是让我头疼。无论如何,这是我迄今为止所做的,我希望有人可以引导我走向正确的方向。

    TreeModel _model = new TreeModel();
    treeViewAdv1.Model = _model;

    treeViewAdv1.BeginUpdate();
    for (int i = 0; i < 20; i++)
    {

        Node parentNode = new Node("root" + i);
        _model.Nodes.Add(parentNode);

        for (int n = 0; n < 2; n++)
        {
            Node childNode = new MyNode("child" + n);
            parentNode.Nodes.Add(childNode); 
        }

    }

    treeViewAdv1.EndUpdate();

无论如何,我得到的只是空节点,我真的无法弄清楚如何将数据添加到其他列...任何帮助将不胜感激...

4 个答案:

答案 0 :(得分:2)

似乎使用这种梦幻般控制的入门价格是数小时和数小时,令人头疼的挫折感。

这是你需要做的:

private void Form1_Load(object sender, EventArgs e)
{
    TreeModel _model = new TreeModel();
    treeViewAdv1.Model = _model;

    treeViewAdv1.BeginUpdate();
    for (int i = 0; i < 20; i++)
    {

        Node parentNode = new Node("root" + i);
        _model.Nodes.Add(parentNode);

        for (int n = 0; n < 2; n++)
        {
            Node childNode = new Node("child" + n);
            parentNode.Nodes.Add(childNode);
        }

    }

    NodeTextBox ntb = new NodeTextBox();
    ntb.DataPropertyName = "Text";       
    this.treeViewAdv1.NodeControls.Add(ntb);

    treeViewAdv1.EndUpdate();


}

答案 1 :(得分:2)

显然这个问题已经过时了,但是我无法找到这个问题的任何答案,所以我想我会试着为某人保留一些未来的挫折感。

要显示多列,请先将UseColumns属性更改为true 然后将所需的columms添加到Columns集合属性中 接下来使用NodeControls集合属性创建将显示的数据类型列表,以及如何格式化它们(Checkbox,TextBox等)。确保设置1)DataPropertyName(稍后将使用),2)ParentColumn(以显示treeView中的哪一列应该显示数据)。 最后,创建一个继承自Node类的新类,并添加一个与每个NodeControl同名的公共属性。然后在添加节点时,请使用具有正确值的新类。

 private class ColumnNode: Node
        {
            public string NodeControl1="";  // This sould make the DataPropertyName specified in the Node Collection.
            public string NodeControl2 = "";
            public string NodeControl3 = "";
            public ColumnNode(string nodeControl1, string nodeControl2, int nodeControl3)
            {
                NodeControl1 = nodeControl1;
                NodeControl2 = nodeControl2;
                NodeControl3 = nodeControl3.ToString();
            }
        }

然后添加时     TreeModel _model = new TreeModel();

_treeViewAdv.Model = _model;
_treeViewAdv.BeginUpdate();
for (int i = 0; i < 20; i++)
{

    Node parentNode = new ColumnNode("root" + i, "",0);
    _model.Nodes.Add(parentNode);

    for (int n = 0; n < 2; n++)
    {
        Node childNode = new ColumnNode("child" + n,"Further Information",1);
        parentNode.Nodes.Add(childNode); 
    }

}
_treeViewAdv.EndUpdate();

作为参考,在这种情况下,treeViewAdv的设计器代码如下所示:

private Aga.Controls.Tree.TreeViewAdv _treeViewAdv;
private Aga.Controls.Tree.TreeColumn Column1;
private Aga.Controls.Tree.TreeColumn Column2;
private Aga.Controls.Tree.TreeColumn Column3;
private Aga.Controls.Tree.NodeControls.NodeTextBox NodeControl1;
private Aga.Controls.Tree.NodeControls.NodeTextBox NodeControl2;
private Aga.Controls.Tree.NodeControls.NodeTextBox NodeControl3;

private InitializeComponent()
{     
    // Left out all other initialization, since this was long enough already.
    this.treeViewAdvPrint = new Aga.Controls.Tree.TreeViewAdv();
    this.Column1 = new Aga.Controls.Tree.TreeColumn();
    this.Column2 = new Aga.Controls.Tree.TreeColumn();
    this.Column3 = new Aga.Controls.Tree.TreeColumn();
    this.NodeControl1 = new Aga.Controls.Tree.NodeControls.NodeTextBox();
    this.NodeControl2 = new Aga.Controls.Tree.NodeControls.NodeTextBox();
    this.NodeControl3= new Aga.Controls.Tree.NodeControls.NodeTextBox();

    // 
    // _treeViewAdv
    // 
    this._treeViewAdv.BackColor = System.Drawing.SystemColors.Window;
    this._treeViewAdv.Columns.Add(this.Column1);
    this._treeViewAdv.Columns.Add(this.Column2);
    this._treeViewAdv.Columns.Add(this.Column3);
    this._treeViewAdv.DefaultToolTipProvider = null;
    this._treeViewAdv.DragDropMarkColor = System.Drawing.Color.Black;
    this._treeViewAdv.GridLineStyle = ((Aga.Controls.Tree.GridLineStyle)((Aga.Controls.Tree.GridLineStyle.Horizontal | Aga.Controls.Tree.GridLineStyle.Vertical)));
    this._treeViewAdv.LineColor = System.Drawing.SystemColors.ControlDark;
    this._treeViewAdv.Location = new System.Drawing.Point(12, 12);
    this._treeViewAdv.Model = null;
    this._treeViewAdv.Name = "_treeViewAdv";
    this._treeViewAdv.NodeControls.Add(NodeControl1);
    this._treeViewAdv.NodeControls.Add(NodeControl2);
    this._treeViewAdv.NodeControls.Add(NodeControl3);
    this._treeViewAdv.SelectedNode = null;
    this._treeViewAdv.Size = new System.Drawing.Size(443, 356);
    this._treeViewAdv.TabIndex = 6;
    this._treeViewAdv.Text = "_treeViewAdv";
    this._treeViewAdv.UseColumns = true;

    // 
    // Column1
    // 
    this.Column1.Header = "Column 1";
    this.Column1.SortOrder = System.Windows.Forms.SortOrder.None;
    this.Column1.TooltipText = null;
    this.Column1.Width = 290;
    // 
    // Column3
    // 
    this.Column3.Header = "Column 3";
    this.Column3.SortOrder = System.Windows.Forms.SortOrder.None;
    this.Column3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    this.Column3.TooltipText = null;
    // 
    // Column2
    // 
    this.Column2.Header = "Column 2";
    this.Column2.SortOrder = System.Windows.Forms.SortOrder.None;
    this.Column2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
    this.Column2.TooltipText = null;
    this.Column2.Width = 91;
    // 
    // NodeControl1
    // 
    this.NodeControl1.DataPropertyName = "NodeControl1";
    this.NodeControl1.IncrementalSearchEnabled = true;
    this.NodeControl1.LeftMargin = 3;
    this.NodeControl1.ParentColumn = this.Column1;
    // 
    // NodeControl2
    // 
    this.NodeControl2.DataPropertyName = "NodeControl2";
    this.NodeControl2.IncrementalSearchEnabled = true;
    this.NodeControl2.LeftMargin = 3;
    this.NodeControl2.ParentColumn = this.Column2;
    // 
    // NodeControl3
    // 
    this.NodeControl3.DataPropertyName = "NodeControl3";
    this.NodeControl3.IncrementalSearchEnabled = true;
    this.NodeControl3.LeftMargin = 3;
    this.NodeControl3.ParentColumn = this.Column3;
}

答案 2 :(得分:0)

您需要先实例化列,然后将它们附加到TreeView。毕竟在相同单元格索引的所有列中添加新单元格。 希望能帮助

答案 3 :(得分:0)

万一你仍在努力解决这个问题,或者其他任何人遇到同样的麻烦:空节点的解决方案是将一个视图组件分配给树。

TreeViewAdv使用MVC,它要求您分配视图,尽管这在示例项目中并不明显。 TreeViewAdv源中NodeControls文件夹下的NodeTextBox类是您可以使用的查看器之一。您需要将其添加到表单并通过其DataPropertyName属性将其连接到树控件,如下所示:

this._tBox.DataPropertyName = "Text";

其中_tBox是NodeTextBox实例。毋庸置疑,我只是浪费了几个小时来搞清楚这一点。希望它可以帮助别人。