【发布时间】:2014-01-28 14:15:20
【问题描述】:
我正在尝试根据其中一个单元格的当前值更改 DataGrid 中行的背景。我的研究使我得出结论,您可以使用 DataTrigger 执行此操作,但我似乎无法编写不会出错的绑定表达式。
该部分的我的 XAML 如下:
<DataGrid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding TodaysBets}" ColumnWidth="*" CanUserAddRows="False" AutoGenerateColumns="False" TextBlock.FontSize="14" IsReadOnly="True">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Matched}" Value="false"> //This binding expression failing
<Setter Property="Background" Value="LightCoral"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
......DataGridColumn Definitions here all bound to "TodaysBets"
</DataGrid.Columns>
</DataGrid>
匹配的列是一个布尔值,它不断抛出绑定错误,例如在对象 DataRowView 上找不到属性 Matched。任何人都可以帮助我尝试了所有方法吗??
它抛出的错误是这样的:
System.Windows.Data Error: 40 : BindingExpression path error: 'Matched' property not found on 'object' ''DataRowView' (HashCode=4932563)'. BindingExpression:Path=Matched; DataItem='DataRowView' (HashCode=4932563); target element is 'DataGridRow' (Name=''); target property is 'NoTarget' (type 'Object')
【问题讨论】:
-
解决方案:创建一个具有适当属性和
INotifyPropertyChanged的适当强类型数据模型,忘记那些古老的、丑陋的、无类型的、基于魔术字符串的数据表。 -
@HighCore 你能详细说明一下吗?我对 WPF 相当陌生,并认为我正在以正确的方式做事。