Bagikan melalui


WPF: Changing control style based on the system theme

Usability is an important aspect when it comes to UI design. The emphasis is on the look and feel. So when it comes to creating your own controls it would be good if they look and feel like rest of the apps - theme dependent. A little zing here and there is good :). Making custom controls theme dependent is pretty easy. All you need is a folder named themes inside which you place the theme file containing the control style. The file name should correspond to the theme.

themes\<Theme Name>.<Theme Color>.xaml

      themes\luna.normalcolor.xaml

      themes\luna.homestead.xaml

      themes\luna.metallic.xaml

      themes\royale.normalcolor.xaml

      themes\aero.normalcolor.xaml

      themes\Classic.xaml

 

Also include a generic.xaml file which is the fallback style

The next step is to include an assembly attribute

[assembly:ThemeInfo(ResourceDictionaryLocation.SourceAssembly, ResourceDictionaryLocation.SourceAssembly)]

 

The first parameter tell the theme engine to look for the theme ResourceDictionary in the current assembly. The second parameter specifies that there is a generic.xaml file which can be used as a fallback. Compile and Run. Change the themes to see it working :)

 

There is an attached zipped file which shows the same for a custom button :) which functions more like a label ;)  .. looks something like the below ..

WindowsApplication3.zip

Comments