WPF中怎么禁用右键菜单?
我在WPF的Treeview中设置了一个ContextMenu,但是我希望在某些情况下让这个菜单不弹出来,请问应该怎么做?
------解决方案--------------------Treeview.ContextMenu.IsEnabled = false;
------解决方案-------------------- if(e.Button == MouseButtons.Right) //鼠标右键
{
if(this.treeView.SelectedNode != null)
{
treeView.ContextMenu = this.contextMenu1;
TreeNodeCollection nodes = this.treeView.Nodes;
}
else
{
}
}
------解决方案--------------------
还有一种方法,在ContextMenu的Opening方法中使用:
void treeView1_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
if (e.Source.GetType() != typeof(TreeViewItem))
{
e.Handled = true;
}
}