CurrentCellChanged事件处理程序中的wpf更改单元格选择无效

CurrentCellChanged事件处理程序中的wpf更改单元格选择无效,wpf,datagrid,selection,cell,Wpf,Datagrid,Selection,Cell,我有一个wpf数据网格,SelectionUnit设置为“CellOrRowHeader”,SelectionMode设置为“Extended” GetCell是一个辅助函数。我验证了单元格不是空的,并且代码被执行。但是,我只看到单元格{3,3}是聚焦的(带有黑色边框),而没有突出显示(没有蓝色背景) 奇怪的是,如果我调用相同的代码,但不是在onCurrentCellChanged事件回调中调用,则单元格{3,3}会高亮显示 有人知道原因吗?非常感谢!除了“CurrentCellChanged”

我有一个wpf数据网格,SelectionUnit设置为“CellOrRowHeader”,SelectionMode设置为“Extended”

GetCell是一个辅助函数。我验证了单元格不是空的,并且代码被执行。但是,我只看到单元格{3,3}是聚焦的(带有黑色边框),而没有突出显示(没有蓝色背景)

奇怪的是,如果我调用相同的代码,但不是在onCurrentCellChanged事件回调中调用,则单元格{3,3}会高亮显示

有人知道原因吗?非常感谢!

除了“CurrentCellChanged”之外,还有另一个事件“SelectedCellsChanged”的作用不同。经过一些测试,我发现这两个事件有不同的用途。例如,在某些情况下,即使选择未更改,也会调用“CurrentCellChanged”。虽然这超出了这个答案的范围

回到主题,我通过在SelectedCellsChanged事件回调中操作单元格选择来解决问题。但有一些技巧:

更改标记:

<DataGrid SelectionUnit="Cell"  SelectionMode="Extended" Name="myDataGrid">
    SelectedCellsChanged="onSelectedCellsChanged" 
</DataGrid>
除了“CurrentCellChanged”之外,还有另一个事件“SelectedCellsChanged”的作用不同。经过一些测试,我发现这两个事件有不同的用途。例如,在某些情况下,即使选择未更改,也会调用“CurrentCellChanged”。虽然这超出了这个答案的范围

回到主题,我通过在SelectedCellsChanged事件回调中操作单元格选择来解决问题。但有一些技巧:

更改标记:

<DataGrid SelectionUnit="Cell"  SelectionMode="Extended" Name="myDataGrid">
    SelectedCellsChanged="onSelectedCellsChanged" 
</DataGrid>
<DataGrid SelectionUnit="Cell"  SelectionMode="Extended" Name="myDataGrid">
    SelectedCellsChanged="onSelectedCellsChanged" 
</DataGrid>
private void onSelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    // trick: Since the code below change the cell selection, which may
    // cause this handler to be called recursively, I need to do some filter
    // If myDataGrid.CurrentCell != e.AddedCells.First(), return

    // Here we can change cell selection
    DataGridCell cell = DataGridHelper.GetCell(myDataGrid, 3, 3);
    cell.IsSelected = true;
}