|
Hi everyone,
I have a scenario that I'm having trouble coming to grips with, and I hoped that someone here might know a solution.
I know that you can have different ResourceDictionaries and combine them, for example:
\Resources\ControlResources.xaml \Resources\WizardResources.xaml \Resources\ConverterResources.xaml
I also know that you can make theme-specific resource dictionaries, and the runtime will decide which one to apply:
\Themes\Luna.NormalColor.xaml \Themes\Aero.NormalColor.xaml \Themes\Classic.xaml \Themes\Generic.xaml
Now, my question is, how can I mix the two?
For example, in my theme-specific resource dictionaries, I'd like to create a bunch of SolidColorBrushes and other settings that change for each theme.
In my Resources, for example ControlResources.xaml, I'd like to define a style for a Button. Something like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource ThemeMyBrush}" /> </Style> </ResourceDictionary>
"ThemeMyBrush" is defined in each of the Themes\* resource dictionaries.
Unfortunately at runtime, everything crashes because the ControlResources.xaml resource dictionary cannot find "ThemeMyBrush". To tell it where to find it, I have to merge the ControlResources.xaml resource dictionary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Themes\Generic.xaml" /> </ResourceDictionary.MergedDictionaries> <Style TargetType="{x:Type Button}"> <Setter Property="Background" Value="{StaticResource ThemeMyBrush}" /> </Style> </ResourceDictionary>
Of course, this works, but it defeats the purpose as it will always apply the "Generic" theme, rather than the correct theme dictionary for the current Windows theme. Instead of merging it with "Generic.xaml", I need to merge it with the "CurrentTheme".
This has to be a common scenario for resource usage, so perhaps I'm approaching it the wrong way. Any help would be appreciated.
(Note: I could define the style for the button in every Theme resource file, but that would be a *lot* of duplication when most of the time the differences between themes are colors and the odd padding/margin setting)
Thanks,
Paul
The shared-source WPF accounting application: www.trialbalance.net.au |