将主题应用于wpf并使用样式

将主题应用于wpf并使用样式,wpf,styles,themes,styling,Wpf,Styles,Themes,Styling,我试图将一个主题应用到一系列wpf项目中。有一个程序集包含generic.xaml和不同的应用程序。据我所知,我不能将ThemeInfo属性与ResourceDictionaryLocation.ExternalLocation一起使用,因为名称必须与我的程序相同,但我有多个程序 因此,我搜索发现,我只需要在app.xaml中将字典作为MergedDictionary包含 <Application.Resources> <ResourceDictionary>

我试图将一个主题应用到一系列wpf项目中。有一个程序集包含generic.xaml和不同的应用程序。据我所知,我不能将ThemeInfo属性与ResourceDictionaryLocation.ExternalLocation一起使用,因为名称必须与我的程序相同,但我有多个程序

因此,我搜索发现,我只需要在app.xaml中将字典作为MergedDictionary包含

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/ClassLibrary1;component/Themes/generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

这基本上是可行的。但是如果我对控件使用样式,它将不再应用generic.xaml样式:

ClassLibrary1.dll中的generic.xaml

<ResourceDictionary x:Class="ClassLibrary1.Themes.generic"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="{x:Type Button}">
    <Setter Property="Background"
            Value="Black" />
</Style>

程序中的窗口

<Window x:Class="Theming.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>
        <Style TargetType="Button"
               x:Key="ButtonGreenTextStyle">
            <Setter Property="Foreground"
                    Value="Green" />
        </Style>
    </Window.Resources>

    <Grid>
        <Button Style="{DynamicResource ButtonGreenTextStyle}" Content="Test" />
    </Grid>
</Window>

我必须做的是,WPF将把generic.xaml中的样式理解为所有按钮的basestyle(我知道我还必须编写一个ControlTemplate;上面的代码只是为了简单起见)

我将尝试两件事

  • 创建常规样式/模板以设置所有样式的外观 钮扣
  • 尝试在ButtonGreeTextStyle上使用属性, 基于现有的风格

    • 我找到了另一个解决方案。您必须基于自定义标记编写样式:

      这将应用当前的主题样式。此标记扩展的代码可在此处找到:


      据我所知,WPF不会为基类应用模板。BasedOn可以工作,但我想知道WPF是否不应该将该样式作为默认样式应用,因为它是在generic.xaml中定义的