wpf - change control template based on current theme -


unfortunately had override control template of 1 of tabcontrols in application because needed slight modification on , feel couldn't done otherwise.

<tabcontrol name="specialtabcontrol" template="{staticresource specialtabcontroltemplate} />".  

all looks fine until switch theme of os. tabcontrollooks totally rubbish because uses control template of wrong theme (the theme based control template on, extracted blend).

so guess need find way provide 4 control templates (luna, aero, xp, classic) special tabcontrol need selected according current os theme.

so how can provide , apply different custom control templates specialtabcontrol based on current theme, when user switches theme of os, specialtabcontrol switch control template have provided theme?

please note have other tabcontrols in application not have override control template , should have standard control template theme.

i think need see themes in wpf application. assembly can contains following lines:

[assembly:themeinfo(resourcedictionarylocation.sourceassembly, resourcedictionarylocation.sourceassembly)]

this means system themes of application located in assembly (in folder /themes). name of themes must comply system themes... example:

the aero theme (windows vista , windows 7): themes\aero.normalcolor.xaml default windows xp theme: themes\luna.normalcolor.xaml olive green windows xp theme: themes\luna.homestead.xaml windows classic theme: themes\classic.xaml 

when user changes skin of system, wpf application automatically download themes. accordingly, can set control template each system theme. more information can found in:

"adam nathan. wpf 4 unleashed". chapter 14.

theming wpf applications: http://blogs.infosupport.com/theming-wpf-applications/

i hope helps.

* edit *

i found interesting example in refers change theme:

http://northhorizon.net/2010/how-to-actually-change-the-system-theme-in-wpf/

you can set explicit style, not respond changes skin in system:

<style x:key="explicitgreenbuttonstyle" targettype="button" basedon="{staticresource {x:type button}}">    <setter property="background" value="green" />    <setter property="foreground" value="white" /> </style> 

accordingly, implicit style respond changes system skin:

<style x:key="implicitgreenbuttonstyle" targettype="button">     <setter property="background" value="green" />     <setter property="foreground" value="white" /> </style> 

also, in example contain useful code @ themehelper, wherein themes functions.

* edit #2 *

if understand correctly, first need system theme name. action can done in several ways.

the first use win32-functions library "uxtheme.dll" getcurrentthemename():

[dllimport("uxtheme.dll", charset = charset.auto)] public static extern int getcurrentthemename(stringbuilder pszthemefilename, int dwmaxnamechars, stringbuilder pszcolorbuff, int dwmaxcolorchars, stringbuilder pszsizebuff, int cchmaxsizechars);  stringbuilder stringthemename = new stringbuilder(260); stringbuilder stringcolorname = new stringbuilder(260); stringbuilder stringsizename = new stringbuilder(260);  int32 s = getcurrentthemename(stringthemename, 260, stringcolorname, 260, stringsizename, 260); 

but have function not correctly received system theme names, such "classic". tried different way.

the second way theme name registry. in path "hkey_current_user\software\microsoft\windows\currentversion\themes\lasttheme" contains current system theme. need intercept "change system theme event" via window hook.

below example few buttons, style varies depending on system theme:

add window:

sourceinitialized="window_sourceinitialized" 

styles:

<style x:key="defaultstyle" basedon="{staticresource {x:type button}}" targettype="{x:type button}">     <setter property="background" value="cadetblue" /> </style>  <style x:key="lunastyle" targettype="{x:type button}">     <setter property="background" value="blue" />     <setter property="foreground" value="white" /> </style>  <style x:key="classicstyle" targettype="{x:type button}">     <setter property="background" value="gray" />     <setter property="foreground" value="black" /> </style> 

main grid:

<grid>     <button style="{staticresource defaultstyle}" content="default button" width="100" height="30" verticalalignment="top" horizontalalignment="left" />     <button name="changebuttonstyle" content="changes style" width="100" height="30" verticalalignment="top" horizontalalignment="right" />     <textblock name="currenttheme" fontsize="16" text="null" width="150" height="30" horizontalalignment="center" verticalalignment="top" /> </grid> 

in code:

intercept "change system theme event":

private intptr hwnd; private hwndsource hsource;  private void window_sourceinitialized(object sender, eventargs e) {     if ((hwnd = new windowinterophelper(this).handle) == intptr.zero)     {         throw new invalidoperationexception("could not window handle.");     }      hsource = hwndsource.fromhwnd(hwnd);     hsource.addhook(wndproc); }  private intptr wndproc(intptr hwnd, int msg, intptr wparam, intptr lparam, ref bool handled) {     switch (msg)     {         case 0x31a:          // define wm_dwmcompositionchanged windows 7         case 0x31e:          // define wm_themechanged          // action on change system theme         getthemename(subkey, value);           return intptr.zero;          default:          return intptr.zero;      } } 

get system theme name , set style:

public string subkey = "software\\microsoft\\windows\\currentversion\\themes\\lasttheme"; public string value = "themefile";  private void getthemename(string openkey, string value) {     registrykey pregkey = registry.currentuser;     pregkey = pregkey.opensubkey(openkey);     object val = pregkey.getvalue(value);     string namethemefile = val string;      if (namethemefile.indexof("luna") != -1)     {         changebuttonstyle.style = this.findresource("lunastyle") style;         currenttheme.text = "luna";     }      if (namethemefile.indexof("classic") != -1)     {         changebuttonstyle.style = this.findresource("classicstyle") style;         currenttheme.text = "classic";     } } 

this method not best, start.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -