循环遍历 Xceed DataGrid for WPF 中的所有单元格?
当用户进行编辑时,我正在更改单元格的背景颜色。我想在保存(或恢复)更改时将所有单元格恢复为正常颜色。
设置单元格的原始背景颜色(存储在父行中)非常容易。但我不知道如何循环遍历表中的所有单元格来重置它们。
我在 Xceed 知识库中找到了一篇文章,名为“如何迭代网格的行“...您认为这是完美的,对吗?错误的;文中提到的.DataRows
、.FixedHeaderRows
等属性(或方法)均来自旧的/已失效的 Xceed 产品。
此论坛帖子建议使用 DataGrid 的 .Items 属性,在我的例子中返回System.Data.DataRowViews...但我找不到任何方法将其(或其任何相关元素)转换为 Xceed.Wpf.DataGrid.DataCells 我需要更改背景颜色。
简而言之,如何循环遍历行和单元格以便重置背景属性?
I am changing the background color of the cells when the user has made an edit. I would like to return all cells to normal colors when the changes are saved (or reverted).
It's easy enough to set the cell's original background color (as stored in the parent row). But I can't figure out how to loop through all the cells in the table to reset them.
I found an article in the Xceed Knowledge Base called "How to iterate through the grid's rows"... which you would think would be perfect, right? Wrong; the properties (or methods) like .DataRows
, .FixedHeaderRows
, etc. mentioned in the article are from an older/defunct Xceed product.
This forum thread recommends using the DataGrid's .Items property, which in my case returns a collection of System.Data.DataRowViews... but I can't find any way to cast that (or any of its related elements) up to the Xceed.Wpf.DataGrid.DataCells I need to change the background color.
In short, how do I loop through the rows and cells so I can reset the background property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢 Xceed 员工 Mohamed,该问题已得到解决发布在 Xceed 论坛上。示例代码如下:
The question has been resolved, thanks to Mohamed, an Xceed employee who posted on the Xceed Forums. Example code follows:
我建议您更改业务逻辑以使用数据绑定。
数据网格中的每个单元格都将是一个对象,它本身知道它是否已被编辑。然后您可以将数据绑定到该属性,因此当您保存并重置所有对象时,状态也将在您的 GUI 中更新。
此外,您还可以免费获得关注点分离。现在,您的 GUI 决定事物的外观,而不是跟踪已保存/未保存的业务逻辑应该是什么。
I propose that you change your business logic to utilize data binding instead.
Each cell in your data grid would then be an object, which itself knows if it has been edited or not. And then you can data bind to that property, and therefore when you save and reset all of your objects, the status will also be updated in your gui.
Also, you get a separation of concerns for free. You GUI now decides how things should LOOK, not what the business logic of tracking saved/not saved should be.
建议的方法是通过隐式样式触发器(由于 UI 虚拟化),并且 Xceed DataGrid 上的所有属性都是可设置的,除了 DataGrid 上定义的主题所强加的属性。
例如:
The recommended way to do this is through an implicit style trigger (because of UI virtualization), and all properties on Xceed DataGrid are settable, except those imposed by the theme defined on the DataGrid.
e.g. :