开发者

Packaging styles in WPF [closed]

Closed. This quest开发者_开发问答ion needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question

I am in the process of creating a new skin for an application written in WPF. Because I am creating new skins for almost all user controls I want to keep it manageable. Here is what I want to do:

Have multiple xaml files for different components of the style (Ex. Global.xaml, Colors.xaml Button.xaml, TabControl.xaml, etc.), They should be able to reference each other. I also want to have one "master" xaml file which includes all the components of the style. This "master" xaml would be the only thing I would have to reference in the App.xaml resources section.


Have a look at ResourceDictionary.MergedDictionaries. It allows you to combine resources defined in separate ResourceDictionaries.

In your situation, Global.xaml, TabControl.xaml, and Button.xaml can each be a ResourceDictionary, and master.xaml can merge them in its MergedDictionaries property:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Button.xaml" />
    <ResourceDictionary Source="TabControl.xaml" />
</ResourceDictionary.MergedDictionaries>

You also want styles to be able to reference styles from other resource dictionaries. You can achieve this in the same way:


Button.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Colors.xaml" /> <!-- we want to use some colors from this resourcedictionary -->
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="Button">
        <Setter Property="Foreground" Value="{StaticResource YourColor}"/>
    </Style>   

You have to be careful though when merging that you don't introduce circular dependencies. If Colors.xaml merged Button.xaml in the example above, you would get a stackoverflowexception when you loaded either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜