3

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

当我在选项卡中移动时,DataGrid 能够保持所选行正确同步,但我遇到的问题是 CurrentCell 始终设置为第一列和第一行,无论 SelectedItem 等于什么。

当 DataGrid 的 DataContext 更改时,我尝试设置 CurrentCell 属性,但 DataGrid 总是将其重置回第一行和第一列。有谁知道当 DataContext 发生变化时,我如何将 CurrentCell 保持在与 SelectedItem 相同的行上?

4

1 回答 1

0

这对我有用(其中 dg 是 DataGrid):

   DataGridCellInfo cellInfo = new DataGridCellInfo(dg.SelectedItem, dg.Columns[0]);
   dg.CurrentCell = cellInfo;
于 2012-11-14T08:39:44.800 回答