wpf Button 左键单击弹出菜单

ContextMenu cmnu = new ContextMenu();
cmnu.Style = null;//可从字典读取
MenuItem item = new MenuItem() { Name = "mnuOpen", Header = "Open" };
item.Click += MenuItem_Click;
cmnu.Items.Add(item);
item = new MenuItem() { Name = "mnuClose", Header = "Close" };
item.Click += MenuItem_Click;
cmnu.Items.Add(item);

btn.Click += (bn, ev) =>
{
	cmnu.Tag = btn.Tag;
    cmnu.PlacementTarget = btn;
    cmnu.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
    cmnu.IsOpen = true;

    //或者 //dg可以改为其它的控件或直接设置ContextMenu
    //dg.ContextMenu.Tag = btn.Tag;
    //dg.ContextMenu.PlacementTarget = btn;
    //dg.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
    //dg.ContextMenu.IsOpen = true;
};
btn.Initialized += (o1, e1) => { btn.ContextMenu = null; };

wpf Button 左键单击弹出菜单_第1张图片

你可能感兴趣的:(c#,wpf)