l33t
l33t

Reputation: 20006

Merge resources into app.xaml?

Targeting .NET Framework 4.0.

I have a UserControl (usercontrol.xaml) with some resources that I would like to be accessible from the application's resources (app.xaml). (My UserControl is instantiated in MainWindow.xaml.)

So far I've tried this in app.xaml:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="usercontrol.xaml"/>
</ResourceDictionary.MergedDictionaries>

But Visual Studio complains about this: An error occurred while finding the resource dictionary "usercontrol.xaml".

Q: How can I add my UserControl's resources to the application so that I can do this in runtime:

Style style = FindResource("SomeStyleDefinedInUserControl") as Style;

Upvotes: 0

Views: 1205

Answers (1)

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9265

Put your styles in a separate resource dictionary if you want to share them.

Then, add the resource dictionary to the merged dictionary of App like you did :

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="usercontrol_styles.xaml"/>
</ResourceDictionary.MergedDictionaries>

The styles are now accessible from anywhere within your application.

Upvotes: 2

Related Questions