title: WPF DataGrid row foreground mouseover color
body: <p>I have been messing around with datagrid styling for a couple of days now, and I am having one issue left which I can not seem to solve.</p>
  
  <p>I want to highligt the text of the row on which I hover with the mouse. Well this seems like an easy task, but at the same time I want custom selection color of the row background and foreground. This combination seems impossible for me to solve.</p>
  
  <p>If I override the datagridrow style I can get everything but the selection style on the row. If I also override the datagridcell style, I can get everything but the mouseover foreground full row highlight. What I have below is complete styling I have come up with so far. I know it contains duplicate attributes for the same tasks, but I have been trying everything. Only the complete row mouseover highlight is missing - only the hovered cell is highlighted.</p>
  
  <p>Can you help me solve this tiny detail?</p>
  
  <p>Put the below code in a WPF Window (I know the coloring is awful, they are just for the example):</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;XmlDataProvider x:Key="persondata"
                       Source="datagrid.xml"
                       XPath="Data" /&gt;
  
      &lt;!-- DataGrid --&gt;
      &lt;Style x:Key="{x:Type DataGrid}"
             TargetType="{x:Type DataGrid}"&gt;
          &lt;Setter Property="AlternatingRowBackground"
                  Value="Orange" /&gt;
          &lt;Setter Property="AlternationCount"
                  Value="2" /&gt;
          &lt;Setter Property="Background"
                  Value="Green" /&gt;
          &lt;Setter Property="BorderThickness"
                  Value="0" /&gt;
          &lt;Setter Property="Foreground"
                  Value="Cyan" /&gt;
          &lt;Setter Property="HeadersVisibility"
                  Value="Column" /&gt;
          &lt;Setter Property="RowBackground"
                  Value="Yellow" /&gt;
      &lt;/Style&gt;
  
      &lt;!-- Column header - remove header background --&gt;
      &lt;Style x:Key="{x:Type DataGridColumnHeadersPresenter}"
             TargetType="{x:Type DataGridColumnHeadersPresenter}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}"&gt;
                      &lt;ItemsPresenter /&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;!-- Column header sort arrow --&gt;
      &lt;Style x:Key="ColumnHeaderSortArrowStyle"
             TargetType="{x:Type Path}"&gt;
          &lt;Setter Property="Data"
                  Value="M0,0 L1,0 0.5,1 z" /&gt;
          &lt;Setter Property="Fill"&gt;
              &lt;Setter.Value&gt;
                  &lt;SolidColorBrush Color="Blue" /&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Setter Property="Height"
                  Value="6" /&gt;
          &lt;Setter Property="Margin"
                  Value="0,0,8,0" /&gt;
          &lt;Setter Property="RenderTransformOrigin"
                  Value="0.5,0.4" /&gt;
          &lt;Setter Property="Stretch"
                  Value="Fill" /&gt;
          &lt;Setter Property="VerticalAlignment"
                  Value="Center" /&gt;
          &lt;Setter Property="Visibility"
                  Value="Collapsed" /&gt;
          &lt;Setter Property="Width"
                  Value="8" /&gt;
      &lt;/Style&gt;
  
      &lt;!-- Column header gripper --&gt;
      &lt;Style x:Key="ColumnHeaderGripperStyle"
             TargetType="{x:Type Thumb}"&gt;
          &lt;Setter Property="Width"
                  Value="8" /&gt;
          &lt;Setter Property="Background"
                  Value="Transparent" /&gt;
          &lt;Setter Property="Cursor"
                  Value="SizeWE" /&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Thumb}"&gt;
                      &lt;Border Background="{TemplateBinding Background}"
                              Padding="{TemplateBinding Padding}" /&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
      &lt;!-- Column header --&gt;
      &lt;Style x:Key="{x:Type DataGridColumnHeader}"
             TargetType="{x:Type DataGridColumnHeader}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type DataGridColumnHeader}"&gt;
                      &lt;Border BorderThickness="0,1,1,0"
                              Background="Black"
                              CornerRadius="5,5,0,0"&gt;
                          &lt;Border.BorderBrush&gt;
                              &lt;SolidColorBrush Color="Red" /&gt;
                          &lt;/Border.BorderBrush&gt;
  
                          &lt;Grid&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="*" /&gt;
                                  &lt;ColumnDefinition Width="Auto" /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition Height="Auto" /&gt;
                              &lt;/Grid.RowDefinitions&gt;
  
                              &lt;Thumb x:Name="PART_LeftHeaderGripper"
                                     Grid.Column="0"
                                     Grid.Row="0"
                                     HorizontalAlignment="Left"
                                     Style="{StaticResource ColumnHeaderGripperStyle}" /&gt;
  
                              &lt;ContentPresenter Grid.Column="0"
                                                Grid.Row="0"
                                                Margin="6,3,6,3"
                                                VerticalAlignment="Center" /&gt;
  
                              &lt;Path x:Name="SortArrow"
                                    Grid.Column="1"
                                    Grid.Row="0"
                                    Style="{StaticResource ColumnHeaderSortArrowStyle}"&gt;
                              &lt;/Path&gt;
  
                              &lt;Thumb x:Name="PART_RightHeaderGripper"
                                     Grid.Column="1"
                                     Grid.Row="0"
                                     HorizontalAlignment="Right"
                                     Style="{StaticResource ColumnHeaderGripperStyle}" /&gt;
                          &lt;/Grid&gt;
                      &lt;/Border&gt;
  
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsMouseOver"
                                   Value="True"&gt;
                              &lt;Setter TargetName="SortArrow"
                                      Property="Fill"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;SolidColorBrush Color="Blue" /&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Trigger&gt;
  
                          &lt;Trigger Property="SortDirection"
                                   Value="Ascending"&gt;
                              &lt;Setter TargetName="SortArrow"
                                      Property="Visibility"
                                      Value="Visible" /&gt;
                              &lt;Setter TargetName="SortArrow"
                                      Property="RenderTransform"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;RotateTransform Angle="180" /&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Trigger&gt;
  
                          &lt;Trigger Property="SortDirection"
                                   Value="Descending"&gt;
                              &lt;Setter TargetName="SortArrow"
                                      Property="Visibility"
                                      Value="Visible" /&gt;
                          &lt;/Trigger&gt;
  
                          &lt;Trigger Property="DisplayIndex"
                                   Value="0"&gt;
                              &lt;Setter Property="Visibility"
                                      Value="Collapsed"
                                      TargetName="PART_LeftHeaderGripper"&gt;&lt;/Setter&gt;
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsMouseOver"
                       Value="True"&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="Blue" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  
      &lt;!-- Row --&gt;
      &lt;Style x:Key="{x:Type DataGridRow}"
             TargetType="{x:Type DataGridRow}"&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="ItemsControl.AlternationIndex"
                       Value="0"&gt;
                  &lt;Setter Property="Background"
                          Value="Yellow" /&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="Black" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="ItemsControl.AlternationIndex"
                       Value="1"&gt;
                  &lt;Setter Property="Background"
                          Value="Orange" /&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="Black" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="IsMouseOver"
                       Value="true"&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="Magenta" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="IsSelected"
                       Value="True"&gt;
                  &lt;Setter Property="Background"
                          Value="Brown" /&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="White" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  
      &lt;!-- Cell --&gt;
      &lt;Style x:Key="{x:Type DataGridCell}"
             TargetType="{x:Type DataGridCell}"&gt;
          &lt;Setter Property="Foreground"&gt;
              &lt;Setter.Value&gt;
                  &lt;SolidColorBrush Color="Black" /&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="IsMouseOver"
                       Value="true"&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="Magenta" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
              &lt;Trigger Property="IsSelected"
                       Value="True"&gt;
                  &lt;Setter Property="Background"
                          Value="Brown" /&gt;
                  &lt;Setter Property="BorderBrush"
                          Value="Brown" /&gt;
                  &lt;Setter Property="Foreground"&gt;
                      &lt;Setter.Value&gt;
                          &lt;SolidColorBrush Color="White" /&gt;
                      &lt;/Setter.Value&gt;
                  &lt;/Setter&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  
  &lt;Grid&gt;
      &lt;DataGrid AutoGenerateColumns="False"
                ItemsSource="{Binding Source={StaticResource persondata}, XPath=Person}"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="First Name"
                                  Binding="{Binding XPath=@Firstname}" /&gt;
              &lt;DataGridTextColumn Header="Last Name"
                                  Binding="{Binding XPath=@Lastname}" /&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>Put the below xml in a file called "datagrid.xml" in the same solution:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;Data&gt;
      &lt;Person Firstname="Fred"
              Lastname="Flintstone"&gt;
      &lt;/Person&gt; 
      &lt;Person Firstname="Barney"
              Lastname="Rubble"&gt;
      &lt;/Person&gt; 
      &lt;Person Firstname="Joe"
              Lastname="Rockhead"&gt;
      &lt;/Person&gt; 
  &lt;/Data&gt;
  </code></pre>
  
  <p>Thank you...</p>