index > Windows Presentation Foundation ("Avalon") > Using merged ResourceDictionaries with theme-specific ResourceDic...

Using merged ResourceDictionaries with theme-specific ResourceDic...

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
Paul Stovell
I think you can define those brushes and other related stuff into a seperate dedicated resource dictionary, and then merge it to the theme dictionary as you wish.

Sheva



WPF C# & Other .Net Related Goodness http://shevaspace.spaces.live.com/
footballism

Hi Sheva,

But then how do I merge the theme dictionaries with the dictionaries that contain my styles?

-- Paul




The shared-source WPF accounting application: www.trialbalance.net.au
Paul Stovell
sorry I misread your original post, you want to merge to the theme dictionary which is currently picked up by the system:)

Sheva





WPF C# & Other .Net Related Goodness http://shevaspace.spaces.live.com/
footballism
reply 4

You can use google to search for other answers

 

More Articles

• How to create an instance of ObservableCollection<T> in XAM...
• getting the june ctp
• MasterPages in WPF
• How to bind values to the Grid in WPF?
• Possible rendering bug when overwriting a Rectangle edge with a L...
• XamlWriter, MarkupObject How to ?
• Databinding dropped in composite control with DP
• Problem:"Cannot locate resource 'window1.xaml'.
• ItemsControl in virtual mode
• PageFunction Base Class
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Resource Dictionary "Adding Stroke/
• Unified way to add and remove child elem
• The name 'InitializeComponent' does not
• MouseEnter + grid row + animation problem
• Animations at GDI speed?
• Loose XAML Template
• Is it possible to do a MasterDetail Bind
• Using a ResourceDictonary for custom con
• Custom control design: errors in template
• Repeating controls
• where can i find 3d shapes in XAML?
• How do you change SelectedItem in a List
• Announcing the WPF/XAML Conversion Tool
• PropertyGrid
• Please help with package signing

Hot Articles

• Unable to animate custom control object'
• RC1 Presentation*.dll trouble with VS 2005
• Set Focus / Selection to child item
• Managing Navigation Exceptions
• Nested/Inner Classes and the x:Type Mark
• How to draw a line in a canvas that stre
• I want trap attached property change not
• Event registration fails with No error o
• Create Image from Background
• Animations at GDI speed?
• how to make my WPF software more efficie
• What dictates object the creation sequen
• Bug in CollectionView
• How to populate a WPF TreeView node from
• How to control line breaking in TextBloc

Recommend Articles

• Change order in Storyboard animation
• ListView GridLines
• XAML RSS Reader problem
• Canvas.SetLeft not working?
• WPF Novice Programmers Catch 22.
• TextBox in DataTeplate...
• Recommended way to make a CheckedListBox
• How to serialize a custom markup extension
• Newbie question about WPF
• Suggestion: Can TextSearch be improved t
• Trigger Error
• About event handler of overlay controls
• How to start ? Where to start?
• multiple animations within one EventTrig
• WPF Bug or What????