Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...