Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPFToolkit DataGrid: Highlight modified rows

Is there a way to highlight all the modified rows on a DataGrid? Since the grid is bound to a System.Data.DataTable I figured I might be able to bind the color of each row to it's RowState (example below), but that doesn't seem to work.

Any ideas?

xmlns:data="clr-namespace:System.Data;assembly=System.Data"
<Style x:Key="DataGridRowStyle" TargetType="{x:Type toolkit:DataGridRow}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter Property="Background" Value="Blue" />
        </Trigger>
        <DataTrigger Binding="{Binding RowState}"
                     Value="{x:Static data:DataRowState.Modified}">
            <Setter Property="Background" Value="LightYellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>
like image 283
Phil Gan Avatar asked Jan 21 '23 13:01

Phil Gan


2 Answers

<DataGrid.RowStyle> 
  <Style TargetType="DataGridRow"> 
    <Style.Triggers> 
        <DataTrigger Binding="{Binding RowState}" Value="{x:Static data:DataRowState.Modified}"> 
            <Setter Property="Background" Value="LightYellow" />            
        </DataTrigger> 
    </Style.Triggers> 
  </Style> 
</DataGrid.RowStyle> 

Update
After you have posted also your xaml, it's obvious that the problem is not to be found in the xaml. I have shortly looked at msdn for the DataTable-class and I can not see a mechanism that let WPF detect changes of the RowState-property. Therefore, direct binding to this property will not give you reliable results.
It seems that you have to wrap your data items. I recommend to make a ViewModel for the items and add a property that says if the row has changed with change notification (INotifyPropertyChanged or DP) and bind to this property. Surely there will be also other alternatives, but IMO creating a VM per item is in most cases the best solution.

like image 166
HCL Avatar answered Jan 31 '23 19:01

HCL


I know this thread is old, but since this issue wasn't fixed in .Net 4, I figured I'd post my workaround. It's a bit clunky (not elegant), but at least it uses data triggers and works.

  1. Add a dummy ModifiedDate column to your DataTable (or if you don't care about the date, just add a dummy column to indicate rowstate).
  2. Add a DataTrigger in XAML referencing the ModifiedDate column and the formatting you want.
  3. Add a DataTrigger in XAML referencing the RowState (this will only take care of Adds and unchanged, so it's one less event to worry about).
  4. Add a RowChanged Event to your ADO.Net Dataset that populates something in your ModifiedDate column (should correspond with your first DataTrigger).
  5. In your saved event that pushes data to your database, blank out your dummy ModifiedDate column at the end (where RowState=unchanged (IMPORTANT: Be sure to check rowstate to verify that no error occured during save.

This works and, best of all, you don't need to do an Items.Refresh every time you modify a row. With that said, if anyone has a better (more elegant) way to accomplish this, then please post.

like image 24
William Avatar answered Jan 31 '23 17:01

William