在C#中,使用WinForms或WPF時,可以通過以下方法將TreeNode數據綁定到控件上:
WinForms:
TreeNode node = new TreeNode("Node Text");
node.Name = "Node Name";
treeView1.Nodes.Add(node);
label1.Text = node.Text;
WPF:
TreeNode node = new TreeNode("Node Text");
node.Header = "Node Name";
treeView1.Nodes.Add(node);
textBlock1.Text = (string)node.Header;
BindingList<TreeNode> bindingList = new BindingList<TreeNode>();
bindingList.Add(node);
treeView1.ItemsSource = bindingList;
textBlock1.Text = bindingList[0].Header;