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;
}
}
}