WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush wpf wpf

WPF Style Trigger for DataGridRow Background Color Trumped by AlternatingRowBackground Brush


There's two ways to do this, neither is particularly obvious. Since DataGridRow transfers (in code) the background property from the parent DataGrid to a local value in the row, as you noted it will take precedence over the value set by your trigger.

The first (and simplest) way is to not use the AlternatingRowBackground or RowBackground but instead use triggers to alternate the background color as Val suggested. His example is not complete though and will not work as-is. The correct style and usage would be as follows. Note that you need to set the AlternationCount on DataGrid or else the rows will never get alternating indexes.

<DataGrid AlternationCount="2">    <DataGrid.RowStyle>        <Style TargetType="DataGridRow">            <Setter Property="Background" Value="White"/>            <Setter Property="FontWeight" Value="Normal"/>            <Style.Triggers>                <Trigger Property="AlternationIndex" Value="1">                    <Setter Property="Background" Value="Wheat"/>                    <Setter Property="FontWeight" Value="Bold"/>                </Trigger>                <Trigger Property="IsMouseOver" Value="True">                    <Setter Property="Background" Value="Khaki"/>                </Trigger>            </Style.Triggers>        </Style>    </DataGrid.RowStyle></DataGrid>

The second option is to use the VisualStateManager. This gives you far more control over the different visual states but it is more verbose. Fortunately it's quite easy to copy the default control template using Blend. Most of the following is unchanged except the Storyboard in the MouseOver state and I've set a background on the selectiveScrollingGrid.

Sorry for the wrap, but like I said, it's a bit more verbose.

<DataGrid AlternationCount="2" AlternatingRowBackground="Wheat">  <DataGrid.RowStyle>    <Style TargetType="DataGridRow">      <Setter Property="Template">        <Setter.Value>          <ControlTemplate TargetType="DataGridRow">            <Border x:Name="DGR_Border"                     Background="{TemplateBinding Background}"                    BorderBrush="{TemplateBinding BorderBrush}"                    BorderThickness="{TemplateBinding BorderThickness}"                    SnapsToDevicePixels="True">              <VisualStateManager.VisualStateGroups>                <VisualStateGroup x:Name="CommonStates">                  <VisualState x:Name="Normal"/>                  <VisualState x:Name="Normal_AlternatingRow"/>                  <VisualState x:Name="Unfocused_Editing"/>                  <VisualState x:Name="Normal_Editing"/>                  <VisualState x:Name="Unfocused_Selected"/>                  <VisualState x:Name="Normal_Selected"/>                  <VisualState x:Name="MouseOver_Unfocused_Editing"/>                  <VisualState x:Name="MouseOver_Editing"/>                  <VisualState x:Name="MouseOver_Unfocused_Selected"/>                  <VisualState x:Name="MouseOver_Selected"/>                  <VisualState x:Name="MouseOver">                    <Storyboard Storyboard.TargetName="Highlight">                      <ColorAnimation Duration="0" Storyboard.TargetProperty="Color" To="Khaki"/>                    </Storyboard>                  </VisualState>                </VisualStateGroup>              </VisualStateManager.VisualStateGroups>              <SelectiveScrollingGrid x:Name="selectiveScrollingGrid">                <SelectiveScrollingGrid.Background>                  <SolidColorBrush x:Name="Highlight" Color="Transparent"/>                </SelectiveScrollingGrid.Background>                <SelectiveScrollingGrid.ColumnDefinitions>                  <ColumnDefinition Width="Auto"/>                  <ColumnDefinition Width="*"/>                </SelectiveScrollingGrid.ColumnDefinitions>                <SelectiveScrollingGrid.RowDefinitions>                  <RowDefinition Height="*"/>                  <RowDefinition Height="Auto"/>                </SelectiveScrollingGrid.RowDefinitions>                <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>                <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>              </SelectiveScrollingGrid>            </Border>          </ControlTemplate>        </Setter.Value>      </Setter>    </Style>  </DataGrid.RowStyle></DataGrid>


What's worked for me in the past with this kind of thing, is to use a setter outside the triggers eg:

<Style TargetType="{x:Type DataGridRow}">    <Style.Triggers>        <Trigger Property="IsMouseOver"                 Value="True">             <Setter Property="Background"                     Value="Red" />             <Setter Property="FontWeight"                     Value="ExtraBold" />             <Setter Property="Height"                     Value="20" />        </Trigger>    </Style.Triggers>    <Setter Property="AlternatingRowBackground"            Value="{DynamicResource AlternatingRow}"/></Style>

And them remove the property binding on the DataGrid itself. Although I usually do this with data triggers, and not usually with dynamic resource bindings. But still might be worth a shot