4 Pluspunkte 0 Minuspunkte

Ich habe diesen Code um beim Klick auf die TreeView Elemente die Properties in einem Inspector anzuzeigen. Derzeit sind das nur einfache Strings. Wie kann ich eine eigene Klasse erstellen und diese im TreeView anzeigen deren Eigenschaften dann im Inspector anzeigen?

using System;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        private TreeView treeView;
        private Panel treePanel;
        private Panel inspectorPanel;
        private PropertyGrid propertyGrid;

        public Form1()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            
            treeView = new TreeView();
            treeView.Dock = DockStyle.Fill;
            treeView.AfterSelect += TreeView_AfterSelect;
            TreeNode rootNode = new TreeNode("Root");
            rootNode.Nodes.Add("Node 1");
            rootNode.Nodes.Add("Node 2");
            treeView.Nodes.Add(rootNode);

            treePanel = new Panel();
            treePanel.Width = 400;
            treePanel.Dock = DockStyle.Left;
            treePanel.Controls.Add(treeView);

            inspectorPanel = new Panel();
            inspectorPanel.Width = 400;
            inspectorPanel.Dock = DockStyle.Right;

            this.Controls.Add(treePanel);
            this.Controls.Add(inspectorPanel);

            propertyGrid = new PropertyGrid();
            propertyGrid.Dock = DockStyle.Fill;
            inspectorPanel.Controls.Add(propertyGrid);
        }

        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            propertyGrid.SelectedObject = e.Node;
        }
    }
}
von  

1 Antwort

2 Pluspunkte 0 Minuspunkte

Hier ist ein Beispiel mit einer eigenen Klasse.

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public class MyData
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public List<string> Hobbies { get; set; }

        public MyData()
        {
            Hobbies = new List<string>();
        }
    }

    public partial class Form1 : Form
    {
        private TreeView treeView;
        private Panel inspectorPanel;
        private PropertyGrid propertyGrid;

        public Form1()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {

            treeView = new TreeView();
            treeView.Dock = DockStyle.Left;
            treeView.Width = 400;
            treeView.AfterSelect += TreeView_AfterSelect;

            TreeNode rootNode = new TreeNode("Root");

            MyData data1 = new MyData { Name = "John", Age = 25 };
            MyData data2 = new MyData { Name = "Jane", Age = 30 };

            rootNode.Nodes.Add(CreateNode("Person 1", data1));
            rootNode.Nodes.Add(CreateNode("Person 2", data2));

            treeView.Nodes.Add(rootNode);

            inspectorPanel = new Panel();
            inspectorPanel.Width = 400;
            inspectorPanel.Dock = DockStyle.Fill;

            propertyGrid = new PropertyGrid();
            propertyGrid.Width = 400;
            propertyGrid.Dock = DockStyle.Right;

            inspectorPanel.Controls.Add(propertyGrid);

            this.Controls.Add(treeView);
            this.Controls.Add(inspectorPanel);

        }

        private TreeNode CreateNode(string nodeName, object tag)
        {
            TreeNode node = new TreeNode(nodeName);
            node.Tag = tag;
            return node;
        }

        private void TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            propertyGrid.SelectedObject = e.Node.Tag;
        }
    }
}
von (732 Punkte)