2
votes

Perhaps somebody will help me out. I have been trying all day to find a solution for sharing styles and resources on my WindowsPhone project.

The problems is the following:

I have a common assembly which is used by two WP projects. Inside this assembly I have some styles and datatemplates defined. I want one of the datatemplates to be defined in the common assembly and be the default datatemplate for the projects.

Assembly resource dictionary sample code:

<Style TargetType="TextBlock" x:Key="TopItemsTitle">
    <Setter Property="FontSize" Value="18"></Setter>
    <Setter Property="FontFamily" Value="Segoe WP Black"></Setter>
    <Setter Property="Foreground" Value="Red"></Setter>
</Style>

<DataTemplate x:Key="TopItemsTemplate">
    <Button>
        <TextBlock x:Name="Name"
                   Grid.Row="1"
                   Width="172"
                   Height="25"
                   Margin="0,6,0,50"
                   VerticalAlignment="Bottom"
                   Text="{Binding Name}"
                   TextWrapping="Wrap" 
                   Style="{StaticResource TopItemsTitle}" />
    </Button>
</DataTemplate>

Inside the different project I would like to redefine the TopItemsTitle style, for example:

<Style TargetType="TextBlock" x:Key="TopItemsTitle">
    <Setter Property="FontSize" Value="20"></Setter>
    <Setter Property="FontFamily" Value="Segoe WP"></Setter>
    <Setter Property="Foreground" Value="Blue"></Setter>
</Style>

and then merge both resource dictionaries in the app.xaml of one the WP projects:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/Assembly;component/Resources/Style.xaml" />
        <ResourceDictionary Source="Resources/LocalStyle.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

According to MSDN this should be possible. However in reality this does not work. The datatemplate does not reflect the redefined style and uses the default one.

Is this a bug or am I simple do it wrong?

Any kind of help will be greatly appreciated.

1

1 Answers

0
votes

I think you are misunderstanding the article. Looking up a StaticResource always starts at the element and then walks up the tree.

TopItemsTemplate is defined in the common Style.xaml, so any {StaticResource} reference in the template will first look for a match in Button (none found), then the DataTemplate (none found), then the ResourceDictionary of Style.xaml (found). If there was nothing found in Style.xaml, only then would it continue to try to find one in the merged dictionary.

If you move the TopItemsTitle to a different dictionary (so the merged dictionary has 3 sources instead of 2), it may work, but I'm not 100% sure.