views:

688

answers:

2

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.

+1  A: 

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.

serialhobbyist
I would agree that the use of MergedDictionaries is a better pattern because it allows the UserControl to have other resources of its own in addition to including external ones. However I think you've missed that fact the Dia is building a Control Library. There is no App.Xaml. Also the output is a dll not a xap hence the sort of Source path you have cannot be used with resource dictionaries added to a Library project. A component resource path is needed.
AnthonyWJones
You're right: by the time I'd gotten to the end, I'd forgotten that.
serialhobbyist
A: 

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!

Dia Bornet