XamlParseException occurred: Failed to create "System.Windows.Style" from the text "PhoneMasterGridColumnHeader"

I have four datagrids on a Silverlight 4 page. I am trying to set different styles of column headings for each grid. I found this XAML that works when I embed it in each DataGrid inside the <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 delete the above tags, it also works by applying to all four headers. But when I try to create a specific version of this 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 an error: XamlParseException occurred: Failed to create "System.Windows.Style" from the text "PhoneMasterGridColumnHeader". I can’t understand what happened. I'm still struggling to deal with styles and resource dictionaries. Any idea?

+6
styles silverlight datagrid resourcedictionary
source share
1 answer

Try it like this: -

  ColumnHeaderStyle="{StaticResource PhoneMasterGridColumnHeader}" 

The ColumnHeaderStyle property expects to receive an object of type Style . Now you can define this value as follows: -

  <DataGrid.ColumnHeaderStyle> <Style TargetType="primitives:DataGridColumnHeade"> <!-- You setters here --> </Style> </DataGrid> 

Or you can, for some reason, call a style that is opened as a property called HeaderStyle in the ViewModel, which is the current DataContext for the grid: -

  <DataGrid ColumnHeaderStyle="{Binding HeaderStyle}" > 

The fact is, in Xaml, you could get multiple property values. A link to a resource is just one of them. Therefore, simply specifying ColumnHeaderStyle="MyStyle" is at least ambiguous, so we need to figure out the state we want to get using static resources.

+7
source share

All Articles