Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit How can I change the column header background color of a DataGrid control?
6 replies. Latest Post by GROY-PREXENS on April 16, 2009.
(0)
John_II
Member
6 points
4 Posts
12-16-2008 9:26 AM |
I am creating a DataGrid control with C#. All I want to do is set the column header background to dark grey and set the foreground color to white. Any help would be much appreciated.
xusun
Participant
991 points
165 Posts
12-16-2008 4:23 PM |
You may be able to set the ForeGround to something else in this way:
Style style = new Style(typeof(System.Windows.Controls.Primitives.DataGridColumnHeader)); style.Setters.Add(new Setter(System.Windows.Controls.Primitives.DataGridColumnHeader.ForegroundProperty, new SolidColorBrush(Colors.White))); dataGrid.ColumnHeaderStyle = style;
But this won't work for the BackGround color since the background there is actually other layers in front of the Element that show you the animations of States and other icons, such as sorting.
If you really want to change everything, you have to re-Style the entire DataGridColumnHeaderStyle.
jeffferson
10 points
5 Posts
01-06-2009 9:41 AM |
how do you re-Style the entire DataGridColumnHeaderStyle?
GROY-PRE...
11 Posts
04-16-2009 9:45 AM |
Hello,
If you have found a solution, i would like have it.
Thank you,
Guillaume
04-16-2009 10:04 AM |
you have to restyle the DataGridColumnHeader.
<Style TargetType="localprimitives:DataGridColumnHeader" > <Setter Property="Foreground" Value="#FF444444" /> <Setter Property="HorizontalContentAlignment" Value="Left" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="FontSize" Value="10.5" /> <Setter Property="FontWeight" Value="Bold" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="SeparatorBrush" Value="#FFC9CACA" /> <Setter Property="Padding" Value="4" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="localprimitives:DataGridColumnHeader" xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"> <Grid Name="Root"> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates" > <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition GeneratedDuration="00:00:0.1" /> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name="Normal" /> <vsm:VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#FF448DCA" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#7FFFFFFF" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#CCFFFFFF" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#F2FFFFFF" /> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="Pressed"> <Storyboard> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#FF448DCA" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#D8FFFFFF" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#C6FFFFFF" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#8CFFFFFF" /> </ColorAnimationUsingKeyFrames> <ColorAnimationUsingKeyFrames Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)"> <SplineColorKeyFrame KeyTime="0" Value="#3FFFFFFF" /> </ColorAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="SortStates" > <vsm:VisualStateGroup.Transitions> <vsm:VisualTransition GeneratedDuration="00:00:0.1" /> </vsm:VisualStateGroup.Transitions> <vsm:VisualState x:Name="Unsorted" /> <vsm:VisualState x:Name="SortAscending"> <Storyboard> <DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" /> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="SortDescending"> <Storyboard> <DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" /> <DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleY)" Duration="0" To="-.9" /> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Rectangle x:Name="BackgroundRectangle" Stretch="Fill" Fill="#FF1F3B53" Grid.ColumnSpan="2" /> <Rectangle x:Name="BackgroundGradient" Stretch="Fill" Grid.ColumnSpan="2" > <Rectangle.Fill> <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1"> <GradientStop Color="#FFFFFFFF" Offset="0.015" /> <GradientStop Color="#F9FFFFFF" Offset="0.375" /> <GradientStop Color="#E5FFFFFF" Offset="0.6" /> <GradientStop Color="#C6FFFFFF" Offset="1" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> <Grid HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <ContentPresenter Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}" /> <Path Grid.Column="1" Name="SortIcon" Fill="#FF444444" RenderTransformOrigin=".5,.5" HorizontalAlignment="Left" VerticalAlignment="Center" Opacity="0" Stretch="Uniform" Width="8" Margin="4,0,0,0" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z "> <Path.RenderTransform> <ScaleTransform ScaleX=".9" ScaleY=".9" /> </Path.RenderTransform> </Path> </Grid> <Rectangle Name="VerticalSeparator" Grid.Column="1" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
04-16-2009 10:24 AM |
Thank you.
I put you code in <UserControl.Resources> </UserControl.Resources> but i have one error with your first line :
<Style TargetType="localprimitives:DataGridColumnHeader" > : "localprimitives:DataGridColumnHeader is not a valide value"
Have you got an idea ?
Regards,
04-16-2009 10:45 AM |
In fact it works, i found where i put it.
Thank you !