如何在WPF应用程序中应用不同的配色方案

如何在WPF应用程序中应用不同的配色方案,wpf,Wpf,有没有办法在运行时替换WPF应用程序的所有笔刷定义,并且只声明使用它一次的样式?这对于不同的配色方案很有用,但要保持相同的UI并声明一次。我能找到的所有示例都在不同的主题文件中复制了样式-这是唯一的方法吗 小例子: Blue.xaml <SolidColorBrush x:Key="DefaultBackgroundBrush" Color="Blue"/> 黄色.xaml <SolidColorBrush x:Key="DefaultBackgroundBrush" C

有没有办法在运行时替换WPF应用程序的所有笔刷定义,并且只声明使用它一次的样式?这对于不同的配色方案很有用,但要保持相同的UI并声明一次。我能找到的所有示例都在不同的主题文件中复制了样式-这是唯一的方法吗

小例子:

Blue.xaml

<SolidColorBrush x:Key="DefaultBackgroundBrush" Color="Blue"/>

黄色.xaml

<SolidColorBrush x:Key="DefaultBackgroundBrush" Color="Yellow"/>

generic.xaml

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

发布问题后,我自己就想出来了,通常是这样;)

下面的代码是用于测试的,所以不要介意它的不性感:

private void MenuItemBlue_Click(object sender, RoutedEventArgs e)
{
    ResourceDictionary genericSkin = new ResourceDictionary();
    genericSkin.Source = new Uri(@"/Themes/" + "generic" + ".xaml", UriKind.Relative);

    ResourceDictionary blueSkin = new ResourceDictionary();
    blueSkin.Source = new Uri(@"/Themes/" + "blue" + ".xaml", UriKind.Relative);

    Application.Current.Resources.MergedDictionaries.Clear();

    Application.Current.Resources.MergedDictionaries.Add(genericSkin);
    Application.Current.Resources.MergedDictionaries.Add(blueSkin);
}
并将“generic.xaml”中定义的样式更改为DynamicSource

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

不过,其他建议也是最受欢迎的