Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe the only way to do this would be to create resource dictionaries for each theme the same you would if you created a custom control and wanted to have a different look for each theme. Then you would create a Style in each for the ComboBox and provide a ResourceKey derived class (e.g. ComponentResourceKey) as the x:Key for the Style using the same value for the x:Key in each theme's resource dictionary. Then when you reference the Style you would use a DynamicResource to that ResourceKey.</p> <p>So a simplified example would be to create a new WpfApplication (e.g. I named its WpfResourceKeys). In this case I'm going to put the theme resource dictionaries in the main assembly so I go into the <strong>AssemblyInfo.cs</strong> and set the <strong>ThemeInfo</strong>'s 1st parameter (i.e. the <strong>themeDictionaryLocation</strong>) to <strong>SourceAssembly</strong>. </p> <p>Then create a folder named "<strong>themes</strong>" and in it create a resource dictionary for each theme you want to support.E.g. <em>aero.normalcolor.xaml</em>, <em>aero2.normalcolor.xaml</em>, <em>luna.normalcolor.xaml</em>, <em>classic.xaml</em>, etc.. </p> <p>In each ResourceDictionary define a Style for ComboBox or whatever control you want and give it an <strong>x:Key</strong> of the same ResourceKey. The easiest thing to use is <strong><a href="http://msdn.microsoft.com/en-us/library/system.windows.componentresourcekey.aspx" rel="nofollow">ComponentResourceKey</a></strong>. In my case I'll use a TextBox since I'll just set the Background and that will be honored regardless of the template defined for each theme. E.g.</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfResourceKeys" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Style TargetType="TextBox" x:Key="{ComponentResourceKey ResourceId=Foo, TypeInTargetAssembly={x:Type local:MainWindow}}"&gt; &lt;Setter Property="Background" Value="Purple" /&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>In my case I just put this into each theme xaml file but with a different value for the Background setter to test it out. So in my aero2.normalcolor.xaml the setter value was Purple and in the classic.xaml the setter value was Orange. When I run my test in Windows 8 with the default theme the TextBox is purple but if I switch to one of the high contrast themes the TextBox is Orange.</p> <p>Then in the place you are going to reference it you would use a DynamicResource instead of a StaticResource since you won't be defining the Style within the resources of the window or app.xaml (because you want the framework to locate it considering the OS theme).</p> <pre><code>&lt;Window x:Class="WpfResourceKeys.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfResourceKeys" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;TextBox Style="{DynamicResource ResourceKey={ComponentResourceKey ResourceId=Foo, TypeInTargetAssembly={x:Type local:MainWindow}}}" Text="ABC" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>You just need to make sure you use an equivalent resource key to how you define it in the theme dictionaries. In the case of ComponentResourceKey that means the ResourceId and TypeInTargetAssembly are equivalent.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload