Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

wpf - how to use styles defined in ResourceDictionary

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}"/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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="ThemesGeneric.xaml"/>

or defined inline:

<Style x:Key="TextViewAllStyle" TargetType="TextBlock">
</Style>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...