4

我正在玩这个名为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

4 回答 4

3

显然这个问题很老,但我无法找到这个问题的任何答案,所以我想我会尝试为某人节省一些未来的挫败感。

要显示多列,首先将 UseColumns 属性更改为 true。
然后将所需的列添加到 Columns 集合属性。
接下来使用 NodeControls 集合属性创建将显示的数据类型的列表,以及它们应该如何格式化(复选框、文本框等)。确保设置 1)DataPropertyName(稍后会用到),2)ParentColumn(显示 treeView 中的哪一列应该显示数据。最后,创建一个继承自 Node 类的新类,并添加一个 public与每个 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;
}
于 2015-01-12T14:44:46.317 回答
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();


}
于 2013-10-09T02:25:26.943 回答
1

以防万一你还在为这个问题苦苦挣扎,或者其他人也有同样的麻烦:空节点的解决方案是将视图组件分配给树。

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

this._tBox.DataPropertyName = "Text";

其中 _tBox 是 NodeTextBox 实例。不用说,我只是浪费了几个小时来弄清楚这一点。希望它可以帮助别人。

于 2013-07-09T16:56:33.263 回答
0

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

于 2012-04-09T12:35:26.773 回答