Trying to use a ResourceDictionary, but styles in it come up as not found
I have a Silverlight class library, called MyClassLibrary.
In it, I have a user control, called MyControl.
Within the control I define user resources:
<UserControl.Resources>
<开发者_运维知识库Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</UserControl.Resources>
The control consumes the style like this:
<ComboBox Style="{ StaticResource ComboBoxStyle }"></ComboBox>
This all works perfectly, the ComboBox comes up with the right style, so I know the style is written correctly.
What I really want is to have the style be in a resource dictionary, so that it can be used by several different controls within this assembly. So I create, in the SAME assembly, a resource dictionary. I call it ResourceDictionary.xaml.
I move the Style definition from my user control to the resource dictionary.
So then the resource dictionary looks like this:
<ResourceDictionary
xmlns="etc" >
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
(lots of xaml)
</Style>
</ResourceDictionary>
The control's user resources now look like this:
<UserControl.Resources>
<ResourceDictionary
Source="/MyClassLibrary;component/ResourceDictionary.xaml" x:Name="resDict"/>
</UserControl.Resources>
And the control still consumes the style exactly the same way as before.
Now I know that it is finding the ResourceDictionary.xaml file, because I tried changing the "Source" attribute to NonExistentFile.xaml, and it complained that it couldn't find the file. It doesn't make that complaint with ResourceDictionary.xaml, so I presume it's finding it.
However, when I run the app, I get an error that "Cannot find a Resource with the Name/Key ComboBoxStyle".
What am I doing wrong? This seems so simple and it's not working.
Thanks in advance for any help you can give me.
Not sure if this helps exactly but I include my ResourceDictionaries in App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Sausage/Bangers.xaml"/>
<ResourceDictionary>
.. other stuff, e.g.
<helpers:NotOperatorValueConverter x:Key="NotOperatorValueConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Even if you don't like the approach, you can see that my Source= is different from yours.
Thank you to both of you, your answers did actually allow me to solve it.
What I really had was something like this:
<UserControl.Resources>
<Style ...> stuff </Style>
</UserControl.Resources>
To which I then added my so it looked like this
<UserControl.Resources>
<Style ...> stuff </Style>
<ResourceDictionary Source="..." />
</UserControl.Resources>
Now this compiled very beatifully but just wouldn't run. I didn't understand that I needed to use MergedDictionaries. So then I got that concept and reorganized the section and now it all works beautifully.
Thanks very much!
精彩评论