C# 在绑定属性上设置WPF DataGrid行的样式
我有一个绑定到EventRecords列表的数据网格。EventRecord对象上有一个属性C# 在绑定属性上设置WPF DataGrid行的样式,c#,wpf,xaml,data-binding,datagrid,C#,Wpf,Xaml,Data Binding,Datagrid,我有一个绑定到EventRecords列表的数据网格。EventRecord对象上有一个属性IsAutoEvent。我正试图根据此属性是否为真来设置行的背景色样式,但DataContext并没有像我预期的那样设置 <DataGrid Name="EventGrid" IsReadOnly="True" AutoGenerateColumns="False" ItemsSource="{Binding Events}" SelectedItem="{Binding SelectedEvent
IsAutoEvent
。我正试图根据此属性是否为真来设置行的背景色样式,但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}
显示视图的属性(例如MainViewModel),而不是DataGridRow(例如EventRecord),正如我所料
有什么想法吗?您没有将
值
放在数据触发器上
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</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>
您没有将
值
置于DataTrigger
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</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>
请将
值添加到数据触发器中
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</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>
请将值添加到数据触发器中
<DataTrigger Binding="{Binding Path=IsAutoEvent}" Value="True">
<Setter Property="Background" Value="Red"/>
</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>
是的,完全正确。根据提交的时间,我接受了@nit的答案。但不管怎样,对你来说+1!是的,完全正确。根据提交的时间,我接受了@nit的答案。但不管怎样,对你来说+1!