I want to set the background color of all the cells of a particular column in WPF Datagrid.
The requirement is pretty straight forward and simple but I am pretty new to WPF.
I found hundred of posts like this Style Only One Column's Cells DataGrid C# WPF which are using DataGridCell
and DataTrigger
combination to set style of a particular cell, but the trigger is always dependent on data on that cell, while I do not want to depend on data but just the column index.
Is there any way to do that?
CodePudding user response:
you can use a simple Style with one setter for Background (individual style for each column):
<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>