当用户进行编辑时,我正在更改单元格的背景颜色。保存(或还原)更改后,我想将所有单元格恢复为正常颜色。
设置单元格的原始背景颜色(存储在父行中)非常容易。但我不知道如何遍历表格中的所有单元格以重置它们。
我在 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/