There are two scenarios:
You select a row and press delete key to delete row.
You select a cell and when its text is highlighted you press delete key to delete text.
How do I distinguish between the two? in both cases I catch PreviewKeyPress and the Key is Delete Key, selected row is same.
For identifying the source of keypress, capture e.OriginalSource
in the PreviewKeyPress(..)
and check whether a cast to DataGridRow
or DatagridCell
is successful.
DataGrid
has the IsReadOnly
property.
Check the selected DataGridCell
's IsEditing
property , perhaps? I assume that, if you've selected a whole row, there will either be no selected cell, or IsEditing will be false on the selected cells (since I don't believe you can edit cells when multiple are selected).
This approach may not work if, when you select a row, it automatically selects, say, the cell in the first column and puts it in editing mode... But I can't test that at the moment.
in OnPreviewKeyDown of the DataGrid:
if ((this.CurrentItem as DataRowView).IsEdit == false && ...)
{
// the row is in view mode
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.