2010-10-01 84 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> 

其他建議最儘管歡迎。

相關問題