【问题标题】:CurrentCell is changed to a row that isn't SelectedItem in DatagridCurrentCell 更改为不是 Datagrid 中 SelectedItem 的行
【发布时间】:2012-10-28 02:38:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf binding datagrid


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。你什么时候能够成功地做到这一点?我尝试在触发 DataContextChanged 以及 CurrentCellChanged 时执行此操作(在这种情况下,我会查找当前单元格的 Item 属性和网格的 SelectedItem 不匹配)。这两种方法似乎都不起作用。
    猜你喜欢
    • 2012-01-04
    • 1970-01-01
    • 2014-08-26
    • 2018-07-18
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多