WPF:根据 WPF Datagrid 中的列索引设置特定列单元格的背景颜色
WPF: Set background color of cells of a particular column based on column index in WPF Datagrid
我想设置 WPF Datagrid 中特定列的所有单元格的背景颜色。
要求非常简单明了,但我是 WPF 的新手。
我发现了数百个像这样 的帖子,它们使用 DataGridCell
和 DataTrigger
组合来设置特定单元格的样式,但触发器始终依赖于该单元格的数据单元格,虽然我不想依赖数据而只是列索引。
有什么办法吗?
您可以使用一个简单的样式,其中一个 setter 用于背景(每列单独的样式):
<DataGridTextColumn Binding="{Binding ...}" Header="Red">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Red"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Green">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Green"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Blue">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Blue"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
我想设置 WPF Datagrid 中特定列的所有单元格的背景颜色。
要求非常简单明了,但我是 WPF 的新手。
我发现了数百个像这样 DataGridCell
和 DataTrigger
组合来设置特定单元格的样式,但触发器始终依赖于该单元格的数据单元格,虽然我不想依赖数据而只是列索引。
有什么办法吗?
您可以使用一个简单的样式,其中一个 setter 用于背景(每列单独的样式):
<DataGridTextColumn Binding="{Binding ...}" Header="Red">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Red"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Green">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Green"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding ...}" Header="Blue">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="Blue"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>