我有一个绑定到EventRecords列表的数据网格。在EventRecord对象上是属性IsAutoEvent
。我尝试在行上设置背景颜色样式,具体取决于此属性是否为true,但DataContext未按预期设置。
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Start Time" Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
<DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
<DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsAutoEvent}">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
目前,这并没有做任何事情。我不认为datacontext是正确的,因为在DataTrigger
下,{Binding}
显示View的属性(例如MainViewModel),而不是DataGridRow(例如EventRecord),正如我所料。
有什么想法吗?
答案 0 :(得分:3)
您尚未将Value
放在DataTrigger
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
答案 1 :(得分:1)
请将Value
添加到DataTrigger
<DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent}" CanUserAddRows="False" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Start Time" Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:hh:mm:ss}}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
<DataGridTextColumn Header="Description" Width="*" Binding="{Binding Description}" CellStyle="{StaticResource CenterAlignedDataGridCell}"/>
<DataGridTextColumn Header="Comments" Width="*" Binding="{Binding Comment}"/>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>