How to implement theming in wpf

回眸只為那壹抹淺笑 提交于 2019-12-30 23:52:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!