对于wpf中的treeview控件,不了解其用法,能否给出一个:
能否给出一个wpf中的treeview控件的右键弹出菜单,并增加节点,删除节点,修改节点内容文字的例子?...
能否给出一个wpf中的treeview控件的右键弹出菜单,并增加节点,删除节点,修改节点内容文字的例子?
展开
1个回答
2013-08-16
展开全部
随便写了一段,测试通过了 public Window1()
{
InitializeComponent();
ContextMenu menu = new ContextMenu();
MenuItem itemAdd = new MenuItem(); itemAdd.Header = "添加节点";
MenuItem itemDel = new MenuItem();
itemDel.Header = "删除节点";
menu.Items.Add(itemAdd);
menu.Items.Add(itemDel);
TreeView treeView = new TreeView();
TreeViewItem root = new TreeViewItem();
root.Header = "根节点";
root.ContextMenu = menu; treeView.Items.Add(root);
this.Content = treeView;
itemAdd.Click += delegate
{
if (treeView.SelectedItem != null)
{
TreeViewItem leave = new TreeViewItem();
leave.Header = "新节点";
TreeViewItem parent = treeView.SelectedItem as TreeViewItem;
parent.Items.Add(leave);
leave.ContextMenu = menu;
}
};
itemDel.Click += delegate
{
if (treeView.SelectedItem != null)
{
TreeViewItem leave = treeView.SelectedItem as TreeViewItem;
if (leave.Parent != null)
{
TreeViewItem parent = leave.Parent as TreeViewItem;
parent.Items.Remove(leave);
}
else
{
treeView.Items.Remove(leave);
}
}
};
}
{
InitializeComponent();
ContextMenu menu = new ContextMenu();
MenuItem itemAdd = new MenuItem(); itemAdd.Header = "添加节点";
MenuItem itemDel = new MenuItem();
itemDel.Header = "删除节点";
menu.Items.Add(itemAdd);
menu.Items.Add(itemDel);
TreeView treeView = new TreeView();
TreeViewItem root = new TreeViewItem();
root.Header = "根节点";
root.ContextMenu = menu; treeView.Items.Add(root);
this.Content = treeView;
itemAdd.Click += delegate
{
if (treeView.SelectedItem != null)
{
TreeViewItem leave = new TreeViewItem();
leave.Header = "新节点";
TreeViewItem parent = treeView.SelectedItem as TreeViewItem;
parent.Items.Add(leave);
leave.ContextMenu = menu;
}
};
itemDel.Click += delegate
{
if (treeView.SelectedItem != null)
{
TreeViewItem leave = treeView.SelectedItem as TreeViewItem;
if (leave.Parent != null)
{
TreeViewItem parent = leave.Parent as TreeViewItem;
parent.Items.Remove(leave);
}
else
{
treeView.Items.Remove(leave);
}
}
};
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询