Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列表填充树视图_C#_Winforms_Treeview - Fatal编程技术网

C# 从列表填充树视图

C# 从列表填充树视图,c#,winforms,treeview,C#,Winforms,Treeview,目前,我在winform中设置了一个硬编码的treeview,其代码如下: private void Form1_Load_1(object sender, EventArgs e) { TreeNode _leftCameraNode = new TreeNode("LeftCamera"); TreeNode _rightCameraNode = new TreeNode("RightCamera"); TreeNode

目前,我在winform中设置了一个硬编码的treeview,其代码如下:

        private void Form1_Load_1(object sender, EventArgs e)
    {
        TreeNode _leftCameraNode = new TreeNode("LeftCamera");
        TreeNode _rightCameraNode = new TreeNode("RightCamera");
        TreeNode[] stereoCameraArray = new TreeNode[] { _leftCameraNode, _rightCameraNode };

        TreeNode _screenNode = new TreeNode("Screen");
        TreeNode _cameraNode = new TreeNode("Camera", stereoCameraArray);

        TreeNode[] headNodeArray = new TreeNode[] { _cameraNode };
        TreeNode _headNode = new TreeNode("HeadNode", headNodeArray);

        TreeNode[] centreNodeArray = new TreeNode[] { _screenNode, _headNode };
        TreeNode _centreNode = new TreeNode("CentreNode", centreNodeArray);

        // root node
        TreeNode[] inmoRootNodeArray = new TreeNode[] { _centreNode };
        TreeNode treeNode = new TreeNode("Main Node", inmoRootNodeArray);
        treeView1.Nodes.Add(treeNode);
    }
这很好,但是它的功能非常有限。我真正想要做的是从我设置的节点列表中填充,并填写从表单上的一堆文本框中获取的信息

例如,我创建了节点类的子屏幕的新列表,如下所示:

    public  List<HV_Screen> _screenList = new List<HV_Screen>();
我一直在网上和stackoverflow上四处寻找,但我找不到任何东西来帮助我动态创建我的treeview,所以我想知道是否有人能为我指出正确的方向,并帮助我如何做到这一点

我想这样做的原因是,假设我有3个不同的屏幕实例,我希望我的treeview显示这个,然后如果我将screen1中的名称更改为MYSCREEN,我希望我的treeview显示这个。但是,如果我单击screen2,treeview仍将显示默认名称。然后,当我在screen1上单击back时,它的新名称MYSCREEN仍将显示在屏幕上

我希望这是有道理的

编辑

下面是我当前GUI的屏幕截图:

那里的树结构都是用上面的代码硬编码的。这不接收来自我的类屏幕类的任何信息

现在,我的HV_屏幕类如下所示:

 private string WIDTH;
    private string HEIGHT;

    public string Width
    {
        get { return WIDTH; }
        set { WIDTH = value; }
    }

    public string Height
    {
        get { return HEIGHT; }
        set { HEIGHT = value; }
    }

    public override List<XmlElement> GenerateXML(XmlDocument _xmlDoc)
    {

       List<XmlElement> elementList = new List<XmlElement>();

        XmlElement _screenName = _xmlDoc.CreateElement("ScreenName");
        _screenName.SetAttribute("Name", name.ToString());
        _screenName.InnerText = name.ToString();
        elementList.Add(_screenName);

        XmlElement _screenTag = _xmlDoc.CreateElement("ScreenTag");
        _screenTag.SetAttribute("Tag", tag.ToString());
        _screenTag.InnerText = tag.ToString();
        elementList.Add(_screenTag);

        XmlElement _localPosition = _xmlDoc.CreateElement("LocalPosition");
        _localPosition.SetAttribute("X", XPOS.ToString());
        _localPosition.SetAttribute("Y", YPOS.ToString());
        _localPosition.SetAttribute("Z", ZPOS.ToString());
        _localPosition.InnerText = WorldPos.ToString();
        elementList.Add(_localPosition);


        XmlElement _orientation = _xmlDoc.CreateElement("Orientation");
        _orientation.SetAttribute("Yaw", YAW.ToString());
        _orientation.SetAttribute("Pitch", PITCH.ToString());
        _orientation.SetAttribute("Roll", ROLL.ToString());
        _orientation.InnerText = Orientation.ToString();
        elementList.Add(_orientation);

        XmlElement _width = _xmlDoc.CreateElement("Width");
        _width.SetAttribute("Width", WIDTH.ToString());
        _width.InnerText = WIDTH.ToString();
        elementList.Add(_width);

        XmlElement _height = _xmlDoc.CreateElement("Height");
        _height.SetAttribute("Height", HEIGHT.ToString());
        _height.InnerText = HEIGHT.ToString();
        elementList.Add(_height);

        return elementList;
    }
没什么大不了的。这只是接收信息并将其写入xml文件。我计划为我的每个节点子节点执行此操作。所以,如果我有一个camera类,它将有自己的generate XML函数并从中写出它。我的基类节点只是有一堆getter和setter,用于名称、标记等

按照目前设置代码的方式,我可以添加新节点。首先,我必须选择要从中进行分支的根节点,然后填写与其关联的信息。所以在那个屏幕截图中,如果我选择主节点,然后点击Add按钮,示例名称现在将是主节点的子节点。然而,若我选择样本和名称并对其进行更改,则不会发生任何事情

这种方式也是硬编码的,只与我的screen类相关。理论上,我可以简单地打开和关闭许多不同的文本框,但这将是非常糟糕的做法。我很肯定这会对我的程序性能产生影响

同样,我想要的是一个名称文本框,它将负责填写每个需要名称的对象的名称。然后,我想把我输入的名字填入树视图。一旦我选择了它,我希望我选择的节点用我为它输入的所有相关文本填写文本框。但是,如果我更改所选节点的名称,它也会更改树结构中的名称


同样,如果这让人困惑,我很抱歉。我现在精疲力竭

HV_屏幕中有什么?…它如何允许您表示树结构?如果你需要帮助,我们需要更多的细节。您的GUI屏幕截图也可能帮助我们更好地理解……我已经输入了更多信息。如果您使用WPF进行此操作,您会更高兴,因为WPF支持真正的数据绑定,并大大简化了此类主/细节场景。
 private string WIDTH;
    private string HEIGHT;

    public string Width
    {
        get { return WIDTH; }
        set { WIDTH = value; }
    }

    public string Height
    {
        get { return HEIGHT; }
        set { HEIGHT = value; }
    }

    public override List<XmlElement> GenerateXML(XmlDocument _xmlDoc)
    {

       List<XmlElement> elementList = new List<XmlElement>();

        XmlElement _screenName = _xmlDoc.CreateElement("ScreenName");
        _screenName.SetAttribute("Name", name.ToString());
        _screenName.InnerText = name.ToString();
        elementList.Add(_screenName);

        XmlElement _screenTag = _xmlDoc.CreateElement("ScreenTag");
        _screenTag.SetAttribute("Tag", tag.ToString());
        _screenTag.InnerText = tag.ToString();
        elementList.Add(_screenTag);

        XmlElement _localPosition = _xmlDoc.CreateElement("LocalPosition");
        _localPosition.SetAttribute("X", XPOS.ToString());
        _localPosition.SetAttribute("Y", YPOS.ToString());
        _localPosition.SetAttribute("Z", ZPOS.ToString());
        _localPosition.InnerText = WorldPos.ToString();
        elementList.Add(_localPosition);


        XmlElement _orientation = _xmlDoc.CreateElement("Orientation");
        _orientation.SetAttribute("Yaw", YAW.ToString());
        _orientation.SetAttribute("Pitch", PITCH.ToString());
        _orientation.SetAttribute("Roll", ROLL.ToString());
        _orientation.InnerText = Orientation.ToString();
        elementList.Add(_orientation);

        XmlElement _width = _xmlDoc.CreateElement("Width");
        _width.SetAttribute("Width", WIDTH.ToString());
        _width.InnerText = WIDTH.ToString();
        elementList.Add(_width);

        XmlElement _height = _xmlDoc.CreateElement("Height");
        _height.SetAttribute("Height", HEIGHT.ToString());
        _height.InnerText = HEIGHT.ToString();
        elementList.Add(_height);

        return elementList;
    }