【问题标题】:Change entire row background in a DataGrid更改 DataGrid 中的整个行背景
【发布时间】:2012-05-09 13:17:27
【问题描述】:
我想将整行的背景与 XAML 中每条记录的布尔属性绑定。
改变数据网格样式的方法太多了,但我想改变负责整行的特定样式...
例如,记录类是数据网格后面的绑定数据,它有一个布尔属性“正确”(真/假),我希望数据网格在红色背景中显示错误正确的行,绿色时真的。
我尝试使用 CellStyle,但它只会更改行中每个单元格的背景,而不是整行。
【问题讨论】:
标签:
wpf
xaml
datagrid
background
styles
【解决方案1】:
如前所述使用DataGrid.RowStyle,例如:
<Style x:Key="DataGridRowCorrectStyle" TargetType="{x:Type Toolkit:DataGridRow}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Correct}" Value="False">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Toolkit:DataGrid RowStyle={StaticResource DataGridRowCorrectStyle} ... />
【解决方案2】:
要更改行的背景颜色,您需要更改行中每个单元格的背景颜色。创建一个设置背景颜色的样式,然后将其分配给 CellStyle 成员。如果要使用 RowStyle 设置颜色,请将单元格的背景颜色设置为透明,然后使用 RowStyle 设置颜色。