68

I have a few WPF applications and I want all my styles to be in a shared assembly instead of declaring them in each application separately.

I am looking for a way so I don't have to change all my Style="{StaticResource BlahBlah}" in the existing applications; I just want to add the reference to this style assembly, and delete it from the current application, so it's taken from the assembly.

Is there any way?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

116

Referencing an external ResourceDictionary (XAML File):

<Application.Resources>
    <ResourceDictionary Source="MyResources.xaml" />
</Application.Resources>

Referencing an external ResourceDictionary (DLL):

<Application.Resources>
    <ResourceDictionary Source="/MyExternalAssembly;component/MyResources.xaml" />
</Application.Resources>
Noam M
  • 3,156
  • 5
  • 26
  • 41
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • But would you please say how we can override a style which exists in the Resources xaml file in other assembly? For Example: A style which has Foreground property set and it's a default style (which doesn't have any x:Key). Then I just want change Background property. So I used BaseOn, but it doesn't work. :( – Amir Karimi Feb 08 '11 at 09:33
  • 3
    @amkh, once that style is imported to the scope you can then redeclare and override it creating a new style at a high scope level setting it's `BasedOn` to `{StaticResource {x:Type TextBox}}` (replacing TextBox with the appropriate type. – Shimmy Weitzhandler Feb 08 '11 at 09:50
  • Thanks a lot. My mistake was that I was creating the Style in an incorrect location while I was using BasedOn. – Amir Karimi Feb 09 '11 at 07:49
  • What about if a UserControl that will be using that resources is in a Class Library Project and there's no App.xaml? – JoanComasFdz Jan 10 '12 at 11:04
  • @JoanComasFdz You could reference that resource dictionary anywhere. You could even add it to that users control resources. But I think maybe you can reference it in `Themes\Generic.xaml` I didn't try it but it might work. – Shimmy Weitzhandler Jan 10 '12 at 13:50
  • @Shimmy Thanks for the answer, but I actually meant "how can I reference it in a WPF UserControl XAML code in a Class Library project where is no "Application.Resources" tag?" – JoanComasFdz Jan 10 '12 at 14:58
  • Refer to [here](http://stackoverflow.com/questions/1228875/what-is-so-special-about-generic-xaml) - applies to Silverlight as well. Add your resources in Generic.xaml. – Shimmy Weitzhandler Jan 29 '12 at 06:46
  • Important thing is Build Action of your Resource file should be set to Resource, and not Embedded Resource. With Embedded Resource it doesn't work. – AnjumSKhan Sep 21 '15 at 05:38
  • How to reference multiple resource files? – Legends Aug 14 '16 at 21:39
  • is there a way to do this without a ResourceDictionary? just loading the external xaml file? – Radinator Feb 17 '17 at 15:48