Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Although the DataGrid does not expose a Header Background property, it does have a property for the ColumnHeaderStyle. Using the technique that DaniCE has previously suggested for a single column we can replace the header template for all header columns including the empty space on the right hand side. The down side with replacing the entire template for a header is that we lose the sorting arrows and separators which are present in the default header template. Fortunately we can use a <a href="http://jesseliberty.com/2008/10/03/template-browser-%E2%80%93-an-indispensible-tool/" rel="nofollow noreferrer">template browser</a> to extract the default template being used and then modify a copy of it.</p> <p>Here I've thrown together a quick example that will change the background of the column headers to LightBlue while keeping the separators and sorting. Take a look at the default DataGridColumnHeader template in a <a href="http://jesseliberty.com/2008/10/03/template-browser-%E2%80%93-an-indispensible-tool/" rel="nofollow noreferrer">template browser</a> to see how to deal with modifying the Background when the mouse hovers over the ColumnHeader.</p> <p><a href="http://i34.tinypic.com/2q2ixch.jpg" rel="nofollow noreferrer">DataGrid Header Background http://i34.tinypic.com/2q2ixch.jpg</a></p> <pre><code>&lt;data:DataGrid x:Name="grid"&gt; &lt;data:DataGrid.ColumnHeaderStyle&gt; &lt;Style xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows" TargetType="primitives:DataGridColumnHeader" &gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="primitives:DataGridColumnHeader"&gt; &lt;Grid Name="Root"&gt; &lt;vsm:VisualStateManager.VisualStateGroups&gt; &lt;vsm:VisualStateGroup x:Name="SortStates" &gt; &lt;vsm:VisualStateGroup.Transitions&gt; &lt;vsm:VisualTransition GeneratedDuration="00:00:0.1" /&gt; &lt;/vsm:VisualStateGroup.Transitions&gt; &lt;vsm:VisualState x:Name="Unsorted" /&gt; &lt;vsm:VisualState x:Name="SortAscending"&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" /&gt; &lt;/Storyboard&gt; &lt;/vsm:VisualState&gt; &lt;vsm:VisualState x:Name="SortDescending"&gt; &lt;Storyboard&gt; &lt;DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" /&gt; &lt;DoubleAnimation Storyboard.TargetName="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" /&gt; &lt;/Storyboard&gt; &lt;/vsm:VisualState&gt; &lt;/vsm:VisualStateGroup&gt; &lt;/vsm:VisualStateManager.VisualStateGroups&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="*" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;ColumnDefinition Width="*" /&gt; &lt;ColumnDefinition Width="Auto" /&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Rectangle x:Name="BackgroundRectangle" Stretch="Fill" Fill="LightBlue" Grid.ColumnSpan="2" Grid.RowSpan="2" /&gt; &lt;ContentPresenter Grid.RowSpan="2" Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" /&gt; &lt;Rectangle Name="VerticalSeparator" Grid.RowSpan="2" Grid.Column="2" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" /&gt; &lt;Path Grid.RowSpan="2" Name="SortIcon" RenderTransformOrigin=".5,.5" HorizontalAlignment="Left" VerticalAlignment="Center" Opacity="0" Grid.Column="1" Stretch="Uniform" Width="8" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z "&gt; &lt;Path.Fill&gt; &lt;SolidColorBrush Color="#FF444444" /&gt; &lt;/Path.Fill&gt; &lt;Path.RenderTransform&gt; &lt;TransformGroup&gt; &lt;ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9" /&gt; &lt;/TransformGroup&gt; &lt;/Path.RenderTransform&gt; &lt;/Path&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/data:DataGrid.ColumnHeaderStyle&gt; &lt;/data:DataGrid&gt; </code></pre> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload