简体   繁体   English

如何确定 wpf datagrid 单元格是否处于编辑模式?

[英]How to determine if wpf datagrid cell is in edit mode?

There are two scenarios:有两种情况:

  1. You select a row and press delete key to delete row.您选择一行并按删除键删除行。

  2. 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.在这两种情况下,我都抓住了 PreviewKeyPress 并且键是删除键,所选行是相同的。

For identifying the source of keypress, capture e.OriginalSource in the PreviewKeyPress(..) and check whether a cast to DataGridRow or DatagridCell is successful.为了识别按键来源,在PreviewKeyPress(..)捕获e.OriginalSource并检查转换到DataGridRowDatagridCell是否成功。

DataGrid has the IsReadOnly property. DataGrid具有IsReadOnly属性。

Check the selected DataGridCell 's IsEditing property , perhaps?检查选定的DataGridCellIsEditing属性,也许? 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).我假设,如果您选择了一整行,则将没有选定的单元格,或者选定单元格上的 IsEditing 将为 false(因为我不相信您可以在选择多个单元格时编辑单元格)。

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:在 DataGrid 的 OnPreviewKeyDown 中:

if ((this.CurrentItem as DataRowView).IsEdit == false && ...)
{
    // the row is in view mode
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM