14

I have several common styles and I want to share them in several pages of my Windows 8.1 application.

I know that I can achieve with merge dictionaries option, but I have no idea how to use styles defined in dictionary.

I tried this:

<Page.Resources>
<ResourceDictionary x:Key="lol">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/Generic.xaml" />
        <ResourceDictionary>
            <Style x:Key="TextViewAllStyle" TargetType="TextBlock">
            </Style>
        </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="TextViewAllStyle2" TargetType="TextBlock">
    </Style>
</ResourceDictionary>
<Style x:Key="TextViewAllStyle3" TargetType="TextBlock">
</Style>
</Page.Resources>

But my Visual Studio sees only the third one...

<TextBlock Style="{StaticResource ResourceKey=TextViewAllStyle3}"/>

1 Answer 1

28

add the following tag to your App.Xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            -- reference your dictionaries here --
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then for -- reference your dictionaries here -- part: each dictionary can be added by its full path:

<ResourceDictionary Source="pack://application:,,,/MySolution.MyProject;component/Theme/Generic.xaml"/>

if in the same project by its relative path:

<ResourceDictionary Source="Themes\Generic.xaml"/>

or defined inline:

<Style x:Key="TextViewAllStyle" TargetType="TextBlock">
</Style>
3
  • Its giving me an error that the resources-property may only be set one time.
    – C4d
    Commented Jun 14, 2016 at 15:47
  • you have probably defined multiple dictionaries or other resources within the resources tag. if you move them to MergedDictionaries it should be fine. see this link stackoverflow.com/a/3425956/366064
    – Bizhan
    Commented Jun 18, 2016 at 17:06
  • Thanks for replying. I've found my problem. Away form the defined dictionary, I still hard some custom styles inside my app.xaml. I just had to surround them with <ResourceDictionary> inside the merde-block. Everything is fine :).
    – C4d
    Commented Jun 20, 2016 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.