Reputation: 896
I am binding datagrid cell background color like this and binding is working.
<DataGridTextColumn MinWidth="50" Header="Count" Binding="{Binding ItemCount}" IsReadOnly="True" CanUserSort="False">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="{Binding Color}"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
But when I bind it like this, it changes text block's background color, not entire cell. Let's say if my item count is over 100, there is no problem. Text block covers whole cell. But if it is less than 100, you can see cell's background because text block can't cover whole cell.
How can I solve this issue?
Upvotes: 1
Views: 483
Reputation: 169420
You should use a CellStyle
to set the background for the cells:
<DataGridTextColumn MinWidth="50" Header="Count" Binding="{Binding ItemCount}" IsReadOnly="True" CanUserSort="False">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding Color}"/>
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Upvotes: 2