views:

38

answers:

1

I have the following style defined in the ResourceDictionary of a Silverlight 4.0 application

<Style x:Key="GridSplitterStyle" TargetType="sdk:GridSplitter">
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Template" Value="{StaticResource GridSplitterTemplate}" />
</Style>

<ControlTemplate x:Key="GridSplitterTemplate" TargetType="sdk:GridSplitter">
    <StackPanel Background="Transparent" Height="32">
        <!-- ... -->
    </StackPanel>
</ControlTemplate>

When I apply the style on my GridSplitter, the style is found and properly applied. However, when linking the Template property to the ControlTemplate defined in the same dictionary file, the following error comes up:

Cannot find a Resource with the Name/Key GridSplitterTemplate

How come Silverlight can find the style but not the template? They are located in the same file...

+1  A: 

Static resource references are resolved during Xaml parsing. As a result you cannot have use forward referencing.

Place the control template above the style in document order so that the parser finds "GridSplitterTemplate" first. Then when "GridSplitterStyle" references it, the parser will be able to find it.

AnthonyWJones
Wow, I didn't see that one coming! I thought these sort of things were resolved since C++.. Thanks heaps.
Xavier Poinas
@Xavier: I would guess that if Xaml were parsed and compiled like other languages then a two-pass system that would allow for forward references would be used. However Xaml parsing is done at runtime not compile time so a faster single pass parse is prefered over the convenience of forward references.
AnthonyWJones