treeview类绑定

时间:2014-11-26 03:30:23

标签: c# winforms treeview

我是c#的新手。 我想用点位置数据进行树视图。 它的树视图如下:..

Form1(父母) Triangle1(child1) Triangle2(child2)

如果我登上Triangle1,那么我想拥有Triangle1的coord信息。 但是,当我制作三角形类并尝试输入treeview节点时...然后发生错误.. 我该如何解决这个问题?


此外.. 公共部分类Form1:表格     {         公共类BigCanvas         {             公共字符串Id;             公共字符串名称;         }

    public class Triangle
    {
        public string Id;
        public string Name;
        public Point point1;
        public Point point2;
        public Point point3;
    }

    public Form1()
    {
        InitializeComponent();
        this.Name = "Draw Triangle";
        this.Text = "Draw sample";
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        BigCanvas bc1 = new BigCanvas();
        bc1.Id = "0";
        bc1.Name = "sample1";
        Triangle ta1 = new Triangle();
        ta1.Id = "01";
        ta1.Name = "triangle1";
        ta1.point1 = new Point(30, 50);
        ta1.point2 = new Point(40,60);
        ta1.point3 = new Point(70,80);

        TreeNode root = treeView1.Nodes.Add(bc1);  // error
        root.Nodes.Add(ta1); // error
        root.Nodes.Add(ta2); // error
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
        TreeNode current = e.Node;

        // Draw with triangle point           
    }

但我不能将Triangle类添加到TreeNode !! ...那么什么是解决这个问题的替代方案..?

1 个答案:

答案 0 :(得分:0)

您可以将节点添加到树:

class MyNode: TreeNode
{
    public string AnotherName { get; set; }
    public int Id{ get; set; }
    public MyNewObjectType NewObject{ get; set; }
    .
    .
}

Tree.Nodes.Add(MyNode);

或使用Node的Tag属性。

node1.Tag = MyClass;
Tree.Nodes.Add(node1);