2014-01-09 73 views
1

我試圖根據其中一個單元格的當前值,在我的DataGrid中更改該行的背景。我的研究讓我得出結論,你可以用DataTrigger做到這一點,但我似乎無法編寫一個不會出錯的綁定表達式。DataTrigger基於DataGrid中的單元格的值綁定到DataView

我對部分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的匹配綁定錯誤。任何人都可以幫助我試過一切?

它被拋出的錯誤是這樣的:

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

+0

解決方案:創建一個適當的強類型數據模型,並使用適當的屬性和INotifyPropertyChanged,並忘記基於古老,醜陋,無類型,基於魔術字符串的DataTable。 –

+0

@HighCore你能詳細點嗎?我對WPF相當陌生,並認爲我正在以正確的方式處理事情。 – TylerD87

回答

0

發現瞭解決這個。所有我需要做的就是改變我的綁定表達式:

Binding="{Binding Row.Matched}" 

由於被選擇DataRowView的結合,所以我不得不做出的綁定表達式選擇底層的DataRow之前,我可以綁定到正確的屬性。

相關問題