循环遍历 Xceed DataGrid for WPF 中的所有单元格?

发布于 2024-08-19 09:34:25 字数 1013 浏览 12 评论 0原文

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

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

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘和柠 2024-08-26 09:34:25

感谢 Xceed 员工 Mohamed,该问题已得到解决发布在 Xceed 论坛上。示例代码如下:

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;
    }
}

The question has been resolved, thanks to Mohamed, an Xceed employee who posted on the Xceed Forums. Example code follows:

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;
    }
}
稚气少女 2024-08-26 09:34:25

我建议您更改业务逻辑以使用数据绑定。

数据网格中的每个单元格都将是一个对象,它本身知道它是否已被编辑。然后您可以将数据绑定到该属性,因此当您保存并重置所有对象时,状态也将在您的 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.

黎夕旧梦 2024-08-26 09:34:25

建议的方法是通过隐式样式触发器(由于 UI 虚拟化),并且 Xceed DataGrid 上的所有属性都是可设置的,除了 DataGrid 上定义的主题所强加的属性。

例如:

  <Style TargetType="{x:Type xcdg:DataCell }">
     <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDirty}"
                     Value="True">
           <Setter Property="Background"
                   Value="DeepSkyBlue" />
        </DataTrigger>
     </Style.Triggers>
  </Style>

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. :

  <Style TargetType="{x:Type xcdg:DataCell }">
     <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsDirty}"
                     Value="True">
           <Setter Property="Background"
                   Value="DeepSkyBlue" />
        </DataTrigger>
     </Style.Triggers>
  </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文