如果记录更改,如何更改 DataGrid 行颜色

发布于 2024-12-18 11:38:54 字数 510 浏览 4 评论 0原文

如果可能的话,我想通过绑定实现以下目标...

我有一个绑定到 ObservableCollection 的 WPF DataGrid

public class Product
{
    public string Code { get; set; }
    public string Desc { get; set; }
    public bool Updated { get; set; }
}

我的网格 DataContext 设置为可观察集合。

我想要实现的目标...

  • 当用户更改数据网格中的一行时,产品的更新字段将更改为“true”。
  • 因此,我可以以某种方式绑定网格的行颜色以显示不同的颜色,这将向用户表明该行尚未保存。

我认为我需要实现 INotifyPropertyChanged 来完成此任务,但不确定具体如何执行。此外,网格上是否有绑定属性来确保 UI 上所做的更改更新后备存储?

干杯。

I would like to achieve the follwing via binding if possible...

I have a WPF DataGrid bound to an ObservableCollection

public class Product
{
    public string Code { get; set; }
    public string Desc { get; set; }
    public bool Updated { get; set; }
}

My grids DataContext is is set to the observable collection.

What I would like to achieve...

  • When the user changes a row in the datagrid the Updated field of the Product is changed to "true".
  • As a result of this I can somehow bind the grid's row color to display a different colour which will indicate to the user that this row hasnt yet been saved.

I think that I will need to implement INotifyPropertyChanged to accomplish this but not certain on exactly how to do it. Additionally, is there a binding property on the grid to ensure that changes made on the UI update the backing store?

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

记忆里有你的影子 2024-12-25 11:38:54
  1. 您的 Product 类应实现 INPC 所以到 Updated 的绑定已更新。

    您还可以更改所有属性的设置器,将 Updated 设置为 true

  2. 您可以在属性上触发:

    
        <样式TargetType =“DataGridRow”>
            <样式.触发器>
                
                    
                
            
        
    
    
  1. Your Product class should implement INPC so bindings to Updated are updated.

    You can additionally change the setters of all properties to set Updated to true.

  2. You can trigger on the property:

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