2010-10-01 110 views
2

有没有办法在运行时替换WPF应用程序的所有画笔定义,并且只声明一次使用它的样式?这对于不同的配色方案会很有用,但保留相同的用户界面并声明一次。我可以找到所有的例子在不同的主题文件中重复样式 - 这是唯一的方法吗?如何在WPF应用程序中应用不同的配色方案

小例子:

Blue.xaml

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

Yellow.xaml

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

generic.xaml?

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

回答

2

想通了自己刚刚张贴的问题后,往往像;)

下面的代码,以用于测试,所以不介意它未sexyness:

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”中定义的样式DynamicResource

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

其他建议最尽管欢迎。

相关问题