我想基于布尔值更改DataGrid的行颜色。我查看了https://stackoverflow.com/questions/18580612/wpf-datagrid-trigger-row-colour-based-on-value并以类似的方式尝试了它(在本地定义样式而不是在usercontrol的资源中定义它),但行背景不会在editable
属性更改。我不知道什么是错的,并且很乐意尝试一些新的东西。
<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
RowBackground="Wheat" AlternatingRowBackground="WhiteSmoke" VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">
<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="LightBlue" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:5)
如果从DataGrid中取出RowBackground和AlternatingRowBackground,它就可以工作。如果要使用它们,则必须将它们放入DataGrid的样式中。
<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch"
VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="RowBackground" Value="Wheat" />
<Setter Property="AlternatingRowBackground" Value="WhiteSmoke" />
</Style>
</DataGrid.Style>
<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable,UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>