我有一個簡單的WPF應用程序來更改顏色主題。WPF Color Theme
ResourceDictionary blueDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Blue/BlueColors.xaml", UriKind.Relative) };
ResourceDictionary greenDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Green/GreenColors.xaml", UriKind.Relative) };
ResourceDictionary yellowDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/Yellow/YellowColors.xaml", UriKind.Relative) };
ResourceDictionary genericDict = new ResourceDictionary() { Source = new Uri(@"/Styles/Colors/GenericColors.xaml", UriKind.Relative) };
在MainWindow上我有一個ComboBox存儲三個枚舉值「藍色,綠色,黃色」。這就是它的時候所選擇的指標發生了變化:
Application.Current.Resources.MergedDictionaries.Clear();
Themes newTheme = (Themes)cbxThemes.SelectedItem;
if (newTheme == currentTheme)
return;
switch (newTheme)
{
case Themes.Blue:
Application.Current.Resources.MergedDictionaries.Add(blueDict);
break;
case Themes.Green:
Application.Current.Resources.MergedDictionaries.Add(greenDict);
break;
case Themes.Yellow:
Application.Current.Resources.MergedDictionaries.Add(yellowDict);
break;
default:
break;
}
Application.Current.Resources.MergedDictionaries.Add(genericDict);
currentTheme = newTheme;
第一次,一切正常,我可以選擇我想要的顏色,但是當我再次改變顏色,沒有任何反應。
有沒有什麼不在後臺更新?
該代碼有效,如果輸出Application.Current.Resources.MergedDictionaries
,您甚至可以看到新的來源。只有用戶界面不會更新。
你是如何引用xaml中的資源的? – MikeT
請參閱https://social.msdn.microsoft.com/Forums/vstudio/en-US/44e80e86-9c62-4b77-87bc-00997a4796f4/change-in-applicationcurrentresourcesmergeddictionaries-is-n--visually-updating?forum=wpf – MikeT