问题
I want to implement ASP.Net type theme in WPF. User have selection of various predefined themes and can switch to another theme.
To implement this I have tried by creating different resourcedictionary for each theme and store required style with key. The collection of keys are same in all themes but its definition is different in each.
I am using below code to switch theme on click:
var theme = Application.LoadComponent(uri) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(theme);
In all pages/ usercontrol/ windows I have set StaticResource and applied appropriate key. When I switch to another theme it is only applied after next event on UI.
If I set change StaticResource to DynamicResource then it is applied on immediately.
Please share your idea to achieve this with StaticResource?
回答1:
have a look at the question
WPF and MVVM - changing themes dynamically
you can download predefined themes from below link
http://wpf.codeplex.com/wikipage?title=WPF%20Themes
回答2:
Unfortunately, you can't achieve this with StaticResource. The Control with the StaticResource needs to be reloaded after switching to another Theme for the new resource to be applied.
You might want to create functionality to reload the current page after change of theme.
来源:https://stackoverflow.com/questions/11150570/how-to-implement-theming-in-wpf