简体   繁体   中英

How to change DataGrid row color if record is changed

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.

  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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM