2012-02-15 3 views
1

アプリケーション全体のカスタムテーマを動的に変更したい。テーマは、ExpressionDark.xamlおよびExpressionLight.xaml(Codeplexからダウンロード)というリソース辞書として提供されます。私は適切なテーマを選択するためにコンボボックスを使用しています。テーマはSelectionChangedイベントで変更されています。WPFを使用してアプリケーション全体に異なるカスタムテーマ(リソース辞書)を適用するにはどうすればよいですか?

private void themesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
ResourceDictionary resourceDictionary = new ResourceDictionary(); 

    int theme = ((ComboBox)sender).SelectedIndex; 

    switch (theme) 
    { 
    case (int)Themes.Dark: 
       resourceDictionary = Application.LoadComponent(
       new Uri(@"Themes\ExpressionDark.xaml", 
       UriKind.Relative)) as ResourceDictionary; 
       break; 
      case (int)Themes.Light: 
       resourceDictionary = Application.LoadComponent(
       new Uri(@"Themes\ExpressionLight.xaml", 
       UriKind.Relative)) as ResourceDictionary; 
       break; 
      default: 
       break; 
    } 

Application.Current.Resources = resourceDictionary; 
} 

これは、現在のウィンドウのために正常に動作しますが、私は別のアプリケーションウィンドウのインスタンスを実行するときにXamlParseExceptionが発生します。ここでは、コードです。

答えて

2
ResourceDictionary skin = new ResourceDictionary(); 
skin.Source = new Uri("Themes\\ExpressionLight.xaml", UriKind.Relative); 
Application.Current.Resources.MergedDictionaries.Clear(); 
Application.Current.Resources.MergedDictionaries.Add(skin); 
+1

ありがとうございます。 –