我有一个WPF DataGrid
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Site" Binding="{Binding Site}" Width="150" />
<DataGridTextColumn Header="Subject" Binding="{Binding Subject}" Width="310" />
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Click="Context_Delete">
<MenuItem.Icon>
<Image Width="12" Height="12" Source="Images/Delete.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
我将点击事件处理程序设为:
private void Context_Delete(object sender, System.EventArgs e) { }
如何在点击之前获取上下文菜单所在的行? sender
对象为System.Windows.Controls.MenuItem
,而不是DataGridRow
。如何获取单击上下文菜单的DataGridRow
。 (我在代码隐藏文件中设置了DataGrid.ItemSource
。)
答案 0 :(得分:29)
因此,基于您的示例代码,我假设您将DataGrid绑定到ObservableCollection对象,并将属性Site和Subject绑定到DataGridColumns。
基本上,您需要做的就是找出绑定到单击的DataGridRow的项目是什么,并从ObservableCollection中删除它。以下是一些示例代码,可帮助您入门:
private void Context_Delete(object sender, RoutedEventArgs e)
{
//Get the clicked MenuItem
var menuItem = (MenuItem)sender;
//Get the ContextMenu to which the menuItem belongs
var contextMenu = (ContextMenu)menuItem.Parent;
//Find the placementTarget
var item = (DataGrid)contextMenu.PlacementTarget;
//Get the underlying item, that you cast to your object that is bound
//to the DataGrid (and has subject and state as property)
var toDeleteFromBindedList = (YourObject)item.SelectedCells[0].Item;
//Remove the toDeleteFromBindedList object from your ObservableCollection
yourObservableCollection.Remove(toDeleteFromBindedList);
}
答案 1 :(得分:6)
通常,您不处理行(如果您这样做 - 再考虑一下原因) - 而是使用视图模型。当您打开上下文菜单时,您将选择您的项目,因此可以通过DataGrid.SelectedItem属性访问它。但是,如果你真的需要DataGridRow - 你有你的DataGrid.SelectedIndex,这里有很多关于如何获取行的答案。比如Get row in datagrid
答案 2 :(得分:3)
为了通过一个例子来扩展morincer的观点,我最终采用了一种更简单的方法......
private void MenuItem_OnClickRemoveSource(object sender, RoutedEventArgs e)
{
if (SourceDataGrid.SelectedItem == null) return; //safety first
_importViewModel.SourceList.Remove((SourceFileInfo)SourceDataGrid.SelectedItem);
}
就我而言,
_importViewModel.SourceList
是行绑定的ObservableCollection。因此,根据最佳实践,我简单地从集合中删除所选项目,绑定负责UI。
答案 3 :(得分:2)
dsfgsho的答案对我有用,但右键单击网格行不会自动选择它。这意味着如果您的焦点位于其他位置并右键单击并选择上下文菜单项,则可以在item.SelectedCells [0]上获得超出范围的异常,或者如果您选择了一行并右键单击另一个排,你可能会得到意想不到的结果。
我通过处理&#34; PreviewMouseRightButtonDown&#34;处理了这个问题。在Datagrid上。在这里,我在右键单击时明确选择一行。我忘了我的UIHelpers类来自哪里(可能在本网站的其他地方 - 我用它来解决拖放项目),但如果你遇到这个问题,这应该指向正确的方向。这是已接受答案的延伸:
// handle right mouse click to select the correct item for context menu usage
private void myDataGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
//find the clicked row
DataGridRow row = UIHelpers.TryFindFromPoint<DataGridRow>((UIElement) sender, e.GetPosition(myDataGrid));
if (row == null)
{
Debug.WriteLine("Row is null");
return;
}
else
{
Debug.WriteLine("Grid Row Index is " + row.GetIndex().ToString());
(sender as DataGrid).SelectedIndex = row.GetIndex();
}
}
答案 4 :(得分:0)
Elemental Pete的UIHelper可能源于:
http://www.hardcodet.net/2009/03/moving-data-grid-rows-using-drag-and-drop
本文列出了一个包含UIHelper.cs的Zip。 这不是我的代码所以没有复制/粘贴。
答案 5 :(得分:-1)
dsfgsho可接受的答案是有道理的,但是当将CommandBinding用于标准ApplicationCommands而不是显式Click事件时,它有所不同,因为发送方不是MenuItem,而是DataGrid本身。
XAML:
<DataGrid.CommandBindings>
<CommandBinding Command="Cut" CanExecute="DataGrid_CanCut" Executed="DataGrid_Cut" />
<CommandBinding Command="Copy" CanExecute="DataGrid_CanCopy" Executed="DataGrid_Copy" />
<CommandBinding Command="Paste" CanExecute="DataGrid_CanPaste" Executed="DataGrid_Paste" />
<CommandBinding Command="New" CanExecute="DataGrid_CanAddNew" Executed="DataGrid_AddNew" />
<CommandBinding Command="Delete" CanExecute="DataGrid_CanDelete" Executed="DataGrid_Delete" />
</DataGrid.CommandBindings>
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
<MenuItem Command="New" />
<MenuItem Command="Delete" />
<Separator />
<MenuItem Header="Test" Command="{Binding CustomContextCommand}" />
</ContextMenu>
</DataGrid.ContextMenu>
后面的代码:
private void DataGrid_Delete(object sender, ExecutedRoutedEventArgs e)
{
// Test whether cleared, resolved, etc., and confirm deletion
var datagrid = (DataGrid)sender;
var trans = (DataClasses.BankTransaction)datagrid.SelectedCells[0].Item;
// Take action here; e.g., remove it from the underlying collection, remove it
// from the DB, etc.
e.Handled = true;
}
private void DataGrid_CanDelete(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}