Reputation: 113
I have been reading up on this for a while now, but I can't seem to find an answer that can help me, so I hope that I will be having more luck here.
In my project I have a Library which contains a lot of styles for controls. These Styles use resources as static resource. So the "resource tree" basically looks like this:
Theme.xaml merges SizeResources, ColorResources and ControlStyles. ControlStyles merge all xamls for the specific controls.
Now in my Application I would like to apply that theme so OnStartup I add the Theme.xaml to my Application.Current.Resources.MergedDictionaries. Which works fine, all keys are there. When I am now trying to use the Controls I get an Exception that for example a Size or Color Resource can not be found. Specific Example:
I have a special button which has a background color that is defined in ColorResources, my ButtonStyle uses this resource as static resource. When I use that Button in my application project I get an exception that the Background resource can not be found. When I change static resource to dynamic resource in control1.xaml then it works.
Any Ideas why I can't do this with static resource? From my understanding, static should work aswell.
Upvotes: 2
Views: 1914
Reputation: 84
Even the dynamic resources have this shortcoming of not being found in the neighbouring merged dictionaries. But the real problem here lies in skinning. What if you only want to change the colours, but not the control templates - which in turn depend on those colours?
One can separate colours and templates into two XAML files, but which "Color" XAML file shall the templates file include, if you have several of these Color files (one per skin)?
Upvotes: 0
Reputation: 178660
If you attempt to define:
<Rectangle Fill="{StaticResource SpecialBackgroundBrush}"/>
instead of your Button
, does it work? I suspect it will. Assuming it does, what you need to do is ensure the Button
style itself has static access to the brushes. Thus, in your Button.xaml you need something like:
<ResourceDictionary ...>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorResources"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button">
...
</Style>
</ResourceDictionary>
It would be useful to see examples of your XAML if this doesn't help.
Upvotes: 2