CurrentCell被更改为Datagrid中没有SelectedItem的行

时间:2021-10-09 09:43:45

I have a DataGrid that is part of a DataTemplate that is assigned to the ContentTemplate of a TabControl. The TabControl's ItemsSource is bound to a collection and, as such, the DataContext for the DataGrid changes to a new collection once each Tab is selected. Currently, there are bindings for ItemsSource and SelectedItem on the DataGrid.

我有一个DataGrid,它是分配给TabControl的ContentTemplate的DataTemplate的一部分。TabControl的ItemsSource绑定到一个集合,因此,一旦每个选项卡被选中,DataGrid的DataContext就会更改为一个新的集合。目前,在DataGrid上有针对ItemsSource和SelectedItem的绑定。

When I move through the tabs the DataGrid is able to keep the selected row synchronized properly but the problem I'm having is that CurrentCell is always set to the first column and first row regardless of what SelectedItem equals.

当我通过制表符时,DataGrid能够正确地保持所选行的同步,但是我遇到的问题是,CurrentCell总是被设置为第一列和第一行,而不管SelectedItem等于什么。

I've tried setting the CurrentCell property when the DataContext changes for the DataGrid but the DataGrid always resets it back to the first row and first column. Does anyone know how I can accomplish keeping the CurrentCell on the same Row as SelectedItem when DataContext changes?

当DataContext更改DataGrid时,我尝试过设置CurrentCell属性,但是DataGrid总是将它重新设置为第一行和第一列。有人知道我如何在DataContext更改时将CurrentCell保持在SelectedItem的同一行吗?

1 个解决方案

#1


0  

This worked for me (where dg is the DataGrid):

这对我很有效(dg是DataGrid):

   DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
   dg.CurrentCell = cellInfo;

#1


0  

This worked for me (where dg is the DataGrid):

这对我很有效(dg是DataGrid):

   DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
   dg.CurrentCell = cellInfo;