Jmyster Jmyster - 1 year ago 90
C# Question

Modify the DataGridCell background color when a style is already set?

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}">
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="LostFocus" Handler="GoalLostFocus" />
<EventSetter Event="LostKeyboardFocus" Handler="GoalLostKeyboardFocus" />
<Grid MinWidth="{Binding Path=ActualWidth, ElementName=colGoalPercentCases}">
<ColumnDefinition Width="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<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"/>

EDIT: Added code for Cell Style - Resolved my issue

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Background" Value="#FFC7D9FF"/>

Answer Source

you can easily change cell style for certain columns via CellStyle property. use BasedOn property of a Style to inherit existing style:

    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="Background" Value="Chocolate"/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download