c# - 遍历 WPF 的 Xceed DataGrid 中的所有单元格?

标签 c# wpf xceed-datagrid

当用户进行编辑时,我正在更改单元格的背景颜色。保存(或还原)更改后,我想将所有单元格恢复为正常颜色。

设置单元格的原始背景颜色(存储在父行中)非常容易。但我不知道如何遍历表格中的所有单元格以重置它们。

我在 Xceed 知识库中找到了一篇名为“How to iterate through the grid's rows”的文章……您会认为它很完美,对吧?错误的;文中提到的.DataRows.FixedHeaderRows等属性(或方法)均来自an older/defunct Xceed product .

This forum thread建议使用 DataGrid 的 .Items 属性,在我的例子中它返回 System.Data.DataRowView 的集合s...但我找不到任何方法将其(或其任何相关元素)转换为 Xceed.Wpf.DataGrid.DataCell s 我需要更改背景颜色。

简而言之,如何遍历行和单元格以便重置背景属性?

最佳答案

问题已解决,感谢Mohamed, an Xceed employee who posted on the Xceed Forums .示例代码如下:

foreach (object item in this.DataGrid1.Items)
{
    Dispatcher.BeginInvoke(new Action<object>(RemoveRowHighlights), DispatcherPriority.ApplicationIdle, item);
}
...
private void RemoveRowHighlights(object item)
{
    Xceed.Wpf.DataGrid.DataRow row = this.DataGrid1.GetContainerFromItem(item) as Xceed.Wpf.DataGrid.DataRow;
    if (row != null) foreach (Xceed.Wpf.DataGrid.DataCell c in row.Cells)
    {
        if (c != null) c.Background = row.Background;
    }
}

关于c# - 遍历 WPF 的 Xceed DataGrid 中的所有单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2110582/

相关文章:

c# - 如何将参数传递给事件触发器wpf中存在的方法

c# - 在 Select 和 Where 调用中重用 Linq to Entities 的 Expression<Func<T, TResult>

c# - 使 WPF 触发器使用任何属性

wpf - 如何使用多个 ViewModel 处理一个 View 并触发不同的命令?

c# - Xceed Datagrid ExportToExcel .xml/.xls/.xlsx 格式

c# - Xceed 数据网格列可见性绑定(bind)

wpf - 将 XceedData Column EditTemplate 绑定(bind)到不同的属性

c# - 如何在 C# 中隐式停止对象中的线程

javascript - 与 .net C# razor 模板进行 react

c# - 我的 BackgroundWorker 仍然阻止我的 UI