private void RightClick(object sender, MouseButtonEventArgs e)
{
DataGrid g = sender as DataGrid;
if (g.ContextMenu != null)
{
g.ContextMenuOpening += G_ContextMenuOpening;
}
}
private void G_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
DataGrid g = sender as DataGrid;
g.ContextMenu.Items.Clear();
MenuItem item1 = MenuItem();
item1.Header = "选项1";
item1.Click += new RoutedEventHandler(item1_Click);
MenuItem item2 = MenuItem();
item2.Header = "选项2";
item2.Click += new RoutedEventHandler(item2_Click);
g.ContextMenu.Items.Add(item1);
g.ContextMenu.Items.Add(item2);
}
void item1_Click(object sender, RoutedEventArgs e)
{
SelectRow=datagrid.SelectedItem;//即能取到SelectedItem的值
}