右击添加上下文菜单
本文关键字:菜单 上下文 添加 右击 | 更新日期: 2023-09-27 18:06:26
public void mytree()
{
DatabaseCore db = new DatabaseCore();
List<allTreeView> myList = new List<allTreeView>();
DataTable dt = db.FillDataGrid();
foreach (DataRow row in dt.Rows)
{
string categories_id1 = row["ID"].ToString();
string parent_id = row["parentId"].ToString();
string categories_name = row["name"].ToString();
myList.Add(new allTreeView(
int.Parse(categories_id1),
categories_name,
int.Parse(parent_id)));
}
Dictionary<int, TreeViewItem> flattenedTree =
new Dictionary<int, TreeViewItem>();
foreach (allTreeView item in myList)
{
TreeViewItem treeNode = new TreeViewItem();
treeNode.Header = item.Header; //this is header
treeNode.Tag = item;
flattenedTree.Add(item.ID, treeNode);
if (flattenedTree.ContainsKey(item.ParentID))
{
flattenedTree[item.ParentID].Items.Add(treeNode);
}
else
{
product_tree.Items.Add(treeNode);
}
}
}
这填充了我的树。我想显示上下文菜单右键点击复制,剪切,选择和添加一些更多的功能在我的树的每个节点。
在xaml中创建一个Treeview,并使用绑定来填充树。这就容易多了。
那么你可以这样做:
<TreeView Name="MyTreeview" .........>
<TreeView.ContextMenu x:Uid="cxt">
<ContextMenu Name="ContextMenu">
<MenuItem Name="AddNew" Header="Add" Click="AddNew_Click"></MenuItem>
<Separator/>
<MenuItem Name="CopyItem" Header="Copy(Ctrl + C)" Click="CopyItemy_Click"> </MenuItem>
</ContextMenu>
</TreeView.ContextMenu>
......
</TreeView>