I have four datagrids on a Silverlight 4 page. I'm trying to set different column header styles for each grid. I found this XAML which works when I embed it in each DataGrid inside <sdk:DataGrid.ColumnHeaderStyle>
tags:
<Style TargetType="primitives:DataGridColumnHeader" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="primitives:DataGridColumnHeader">
<Grid Name="Root">
<vsm:VisualStateManager.VisualStateGroups>
<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="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" />
</Storyboard>
</vsm:VisualState>
</vsm:VisualStateGroup>
</vsm:VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Rectangle x:Name="BackgroundRectangle" Stretch="Fill" Grid.ColumnSpan="2" Grid.RowSpan="2">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Transparent" Offset="0" />
<GradientStop Color="LavenderBlush" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<ContentPresenter Grid.RowSpan="2" Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" />
<Rectangle Name="VerticalSeparator" Grid.RowSpan="2" Grid.Column="2" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" />
<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 ">
<Path.Fill>
<SolidColorBrush Color="#FF444444" />
</Path.Fill>
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9" />
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When I put it in a separate resource dictionary and remove the aforementioned tags, it also works, applying to all four headers. But when I try to create a specific version of it in the resource dictionary but changing the first line to this:
<Style x:Key="ADGridColumnHeader" TargetType="primitives:DataGridColumnHeader" >
and adding this to the DataGrid:
ColumnHeaderStyle="PhoneMasterGridColumnHeader"
I get the error: XamlParseException occurred: Failed to create a 'System.Windows.Style' from the text 'PhoneMasterGridColumnHeader'. I can't work out what's wrong. I'm still struggling to get to grips with Styles and Resource Dictionaries. Any idea?
Try it like this:-
ColumnHeaderStyle="{StaticResource PhoneMasterGridColumnHeader}"
The ColumnHeaderStyle property is expecting to receive an object of type Style
. Now you could define that value like this:-
<DataGrid.ColumnHeaderStyle>
<Style TargetType="primitives:DataGridColumnHeade">
<!-- You setters here -->
</Style>
</DataGrid>
Or you might for some bizare reason have the style exposed as a property called HeaderStyle
on a ViewModel which is the current DataContext
for the grid:-
<DataGrid ColumnHeaderStyle="{Binding HeaderStyle}" >
The point is that there are several different ways property values could be acquired in Xaml. Referencing a resource is just one of them. Hence simply stating ColumnHeaderStyle="MyStyle"
is at least ambiguous so we need to explict state that we want to acquire the value via static resources.