【问题标题】:Put all the cells of selected row of a DataGrid in Edit Mode in WPF将 DataGrid 选定行的所有单元格置于 WPF 中的编辑模式
【发布时间】:2014-09-27 14:08:57
【问题描述】:

如标题所述,我想在编辑模式下获取 selectedRow 的所有单元格。

到目前为止,我已经尝试获取 dataGrid 的当前单元格和当前行,如下面的代码中所述。

private void DataGrid_Edit_Click(object sender, RoutedEventArgs e)
{
    int colIndex = 0;
    int rowIndex = 0;

    DependencyObject dep = (DependencyObject)e.OriginalSource;
    while (dep != null && !(dep is DataGridCell))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    if (dep is DataGridCell)
    {

        colIndex = ((DataGridCell)dep).Column.DisplayIndex;

        while (dep != null && !(dep is DataGridRow))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        DataGridRow row = (DataGridRow)dep;
        rowIndex = FindRowIndex(row);
    }

    while (dep != null && !(dep is DataGrid))
    {
        dep = VisualTreeHelper.GetParent(dep);
    }

    if (dep == null)
        return;

    DataGrid dg = (DataGrid)dep;

    for (int column = 0; column < colIndex; column++)
    {
        dg.CurrentCell = new DataGridCellInfo(dg.Items[rowIndex], dg.Columns[column]);
        dg.BeginEdit();
    }

}

上面的代码就像它遍历指定行的所有单元格一样工作。但最后最后一个单元格进入编辑模式,所以我无法让以前的单元格处于编辑模式。

您能否建议对 selectedRow 的所有单元格而不是我得到的最后一个单元格执行相同的操作?

【问题讨论】:

  • 循环遍历选定行的所有单元格并将单元格的IsEditing 属性设置为true
  • @RohitVats 我错过了。我不知道 DataGridCell 上有任何名为 IsEditing 的属性。相反,我正在设置 DataGrid 的当前单元格。谢谢你。现在可以正常使用了。
  • 太好了,我已将其转换为答案,以便它也可以帮助其他人。

标签: c# wpf datagrid


【解决方案1】:

BeginEdit() 会将当前单元格置于编辑模式并结束已处于编辑模式的任何单元格的编辑模式。这就是为什么您看到最后一个单元格始终处于编辑模式的原因。

因此,作为一种解决方法,您可以做的是遍历选定行的所有单元格并将单元格的 IsEditing 属性设置为 true。

【讨论】:

  • 再次感谢。是的,这个答案将来可能会对某人有所帮助。
  • @Rohit Vats 对于循环遍历每个单元格,如何获得选定行的Cell 对象(即DataGridCell)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 2011-11-27
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多