【问题标题】:DataTrigger based on value of cell in DataGrid bound to DataViewDataTrigger 基于绑定到 DataView 的 DataGrid 中单元格的值
【发布时间】: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 相当陌生,并认为我正在以正确的方式做事。

标签: c# wpf xaml datagrid


【解决方案1】:

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

Binding="{Binding Row.Matched}" 

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

【讨论】:

    猜你喜欢
    • 2017-08-07
    • 2016-02-24
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2010-10-24
    • 2013-12-27
    • 2014-09-04
    相关资源
    最近更新 更多