WPF将主题应用于特定项目而不是整个应用程序

时间:2013-06-28 20:05:06

标签: wpf c#-4.0 mvvm

我在project1中创建了一个主题,并在app.xaml中引用了theme.xaml。效果是所有项目都在解决方案中获得相同的主题。

将theme.xaml应用于指定项目的最简单方法是什么,即仅对project1而不是project2?

我知道我可以使用

在project1中的每个WFP表单中引用theme.xaml
    <Window.Resources>
        <ResourceDictionary Source="/Project1;component/Themes/Customized.xaml" />
    </Window.Resources>

但如果我想改变项目的主题,那就有点难以维护。我正在寻找的是类似于app.xaml的project.xaml,只有当前项目的范围。这样我可以在一个地方为指定的项目引用theme.xaml(但不是其他项目)。

这可能吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

  1. 创建项目主题资源字典并将FooTheme.xaml引用到其中。

  2. 在项目的所有窗口中,引用ProjectTheme.xaml

  3. 这样,为了更改项目的主题,您只需要修改一行。

    代码:

    FooTheme.xaml (示例主题)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style TargetType="Button">
            <Setter Property="Background" Value="Blue"/>
        </Style>
    </ResourceDictionary>
    

    ProjectTheme.xaml (项目主题)

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <ResourceDictionary.MergedDictionaries>
            <!-- In order to modify the project's theme, change this line -->
            <ResourceDictionary Source="FooTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    

    MainWindow.xaml (示例项目窗口)

    <Window x:Class="So17372811ProjectTheme.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="ProjectTheme.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
        <Grid>
            <Button Content="Click me!"/>
        </Grid>
    </Window>
    
相关问题