Reputation: 995
I'm not much of a UI guy but I have been task with what seemed to be a simple thing to do.
I need to change a few of the column backgrounds of an already established working datagrid for what the users say will be easier to read. Everything I read points to using <Style TargetType="DataGridCell">
. This would make sense and easily doable except there is a sty;e already applied to a different target and I can't add another.
How can I modify the DataGridCell background color when a style is already set?
This is what one of the columns looks like now
<DataGridTextColumn x:Name="colGoalPercentCases" u:XAMLProperties.GroupName="Cases" Width="*" IsReadOnly="False"
Binding="{Binding Path=GoalPercent_Cases, TargetNullValue='', Mode=TwoWay, StringFormat='{}{0:#,#.00\\%;-#,#.00\\%}',NotifyOnTargetUpdated=True}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="LostFocus" Handler="GoalLostFocus" />
<EventSetter Event="LostKeyboardFocus" Handler="GoalLostKeyboardFocus" />
</Style>
</DataGridTextColumn.EditingElementStyle>
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Grid MinWidth="{Binding Path=ActualWidth, ElementName=colGoalPercentCases}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="CS %" Margin="1,0,10,0" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"/>
<TextBox x:Name="tbCasePercent" Margin="1,0,10,0" IsReadOnly="True" Grid.Row="1" Grid.Column="0" Width="Auto" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right"/>
</Grid>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
EDIT: Added code for Cell Style - Resolved my issue
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Background" Value="#FFC7D9FF"/>
</Style>
</DataGridTextColumn.CellStyle>
Upvotes: 0
Views: 206
Reputation: 35722
you can easily change cell style for certain columns via CellStyle
property. use BasedOn
property of a Style to inherit existing style:
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Background" Value="Chocolate"/>
</Style>
</DataGridTextColumn.CellStyle>
Upvotes: 2