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

时间:2021-12-03 22:16:43

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并且Key是Delete Key,所选行是相同的。

2 个解决方案

#1


7  

For identifying the source of keypress, capture e.OriginalSource in the PreviewKeyPress(..) and check whether a cast to DataGridRow or DatagridCell is successful.

要识别keypress的来源,请在PreviewKeyPress(..)中捕获e.OriginalSource,并检查对DataGridRow或DatagridCell的强制转换是否成功。

DataGrid has the IsReadOnly property.

DataGrid具有IsReadOnly属性。

#2


0  

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).

或许,检查所选的DataGridCell的IsEditing属性?我假设,如果你选择了一整行,那么将不会选择任何单元格,或者所选单元格上的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.

如果在选择一行时,它会自动选择第一列中的单元格并将其置于编辑模式,这种方法可能无法正常工作......但我现在无法测试。

#1


7  

For identifying the source of keypress, capture e.OriginalSource in the PreviewKeyPress(..) and check whether a cast to DataGridRow or DatagridCell is successful.

要识别keypress的来源,请在PreviewKeyPress(..)中捕获e.OriginalSource,并检查对DataGridRow或DatagridCell的强制转换是否成功。

DataGrid has the IsReadOnly property.

DataGrid具有IsReadOnly属性。

#2


0  

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).

或许,检查所选的DataGridCell的IsEditing属性?我假设,如果你选择了一整行,那么将不会选择任何单元格,或者所选单元格上的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.

如果在选择一行时,它会自动选择第一列中的单元格并将其置于编辑模式,这种方法可能无法正常工作......但我现在无法测试。