[WPF]DataGrid C#添加右键弹出选择菜单

时间:2022-02-15 03:52:08
private void dataGrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ContextMenu context = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "点击删除该行数据";
item.Click += new RoutedEventHandler(item_Click);
context.Items.Add(item);
context.IsOpen = true;
}
void item_Click(object sender, RoutedEventArgs e)
{
// do something
}
//方法2 private void ContextMenuLoad()
{
ContextMenu context = new ContextMenu();
MenuItem item = new MenuItem();
item.Header = "点击删除该行数据";
item.Click += new RoutedEventHandler(item_Click);
context.Items.Add(item);
context.IsOpen = true;
ContextMenuService.SetContextMenu(datagrid, context);
}