WPFToolkit DataGrid:突出显示修改的行 [英] WPFToolkit DataGrid: Highlight modified rows
问题描述
有没有办法在 DataGrid
上突出显示所有修改过的行?由于网格绑定到 System.Data.DataTable
我想我可以将每一行的颜色绑定到它的 RowState
(下面的示例),但这并没有似乎不起作用.
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.
有什么想法吗?
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>
推荐答案
<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>
更新
在您还发布了您的 xaml 之后,很明显在 xaml 中找不到问题.我刚刚查看了 DataTable 类的 msdn,但我看不到让 WPF 检测 RowState
属性更改的机制.因此,直接绑定到此属性不会为您提供可靠的结果.
看来您必须包装您的数据项.我建议为项目创建一个 ViewModel 并添加一个属性,该属性表示该行是否已通过更改通知(INotifyPropertyChanged
或 DP)更改并绑定到此属性.当然还有其他选择,但 IMO 为每个项目创建一个 VM 在大多数情况下是最好的解决方案.
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.
这篇关于WPFToolkit DataGrid:突出显示修改的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!