首页 文章

DataTrigger基于DataGrid中绑定到DataView的单元格的值

提问于
浏览
1

我试图根据其中一个单元格的当前值更改 DataGrid 中行的背景 . 我的研究让我得出结论,你可以用 DataTrigger 做到这一点,但我可以't seem to write a binding expression that won' t错误 .

我的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')

1 回答

  • 0

    找到解决方案 . 我所要做的就是将绑定表达式更改为:

    Binding="{Binding Row.Matched}"
    

    因为绑定是选择DataRowView所以我必须使绑定表达式选择底层DataRow,然后才能绑定到正确的属性 .

相关问题