One of the nice features about WPF is how easily skin-able it makes your applications. You can store information about colors or control styles within a resource dictionary, and load these at the start of your program, or at runtime.
Here is an example of how to unload the resource dictionary you're currently using, and load a new one on the fly to change the look of your application:
1void Window4_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
2{
3 String source = null;
4
5 if (e.Key == System.Windows.Input.Key.F5)
6 {
7 source = "Dictionary1.xaml";
8 }
9 else if (e.Key == System.Windows.Input.Key.F6)
10 {
11 source = "Dictionary2.xaml";
12 }
13 else if (e.Key == System.Windows.Input.Key.F7)
14 {
15 source = "Dictionary3.xaml";
16 }
17
18 if (source != null)
19 {
20 ResourceDictionary dict = new ResourceDictionary();
21 dict.Source = new Uri(source, UriKind.Relative);
22
23 Application.Current.Resources.MergedDictionaries.Clear();
24 Application.Current.Resources.MergedDictionaries.Add(dict);
25 }
26}
Note that I've made the assumption here that you are loading your resources at the application level, and thus these changes will affect your entire application. If you wanted to effect it at a Window level, you can just as easily get the Resources of the Window and do the same exact thing.
In this example, the user can push F5-F7 and get a different look to their program. You may want to add something to check the selected dictionary against the chosen one, but here is my result of changing resource dictionaries in this way:


0 comments:
Post a Comment