示例:
<Button Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Content="按钮_具有右键菜单">
<Button.ContextMenu>
<ContextMenu StaysOpen="true">
<MenuItem Header="File"/>
<MenuItem Header="Save"/>
<Separator/>
<MenuItem Header="Recent Files">
<MenuItem Header="ReadMe.txt"/>
<MenuItem Header="Schedule.xls"/>
</MenuItem>
</ContextMenu>
</Button.ContextMenu>
</Button>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
后台:
Dictionary<string, RoutedEventHandler> dist = new Dictionary<string, RoutedEventHandler>();
//添加右键菜单项,包括名称和事件
dist.Add("添加", add_Click);
dist.Add("修改", edit_Click);
dist.Add("删除", del_Click);
dist.Add("导出", putout_Click);
dgrid.ContextMenu = ContextMenus(dist);
#region 循环生成右键菜单
private ContextMenu ContextMenus(Dictionary<string, RoutedEventHandler> list)
{
ContextMenu cm = new ContextMenu();
//Style style = (Style)this.FindResource("ContextMenu"); //定义前台显示模板
//cm.Style = style;
foreach (var dc in list)
{
MenuItem menu = new MenuItem();
//menu.Style = (Style)this.FindResource("JjxyMenuItem");
menu.Header = dc.Key;
menu.Click += dc.Value;
cm.Items.Add(menu);
}
return cm;
}
#endregion 循环生成右键菜单
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
作者:꧁执笔小白꧂