Load WPF styles or other Static Resources from an external file or assembly

62,362

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>
Share:
62,362
Shimmy Weitzhandler
Author by

Shimmy Weitzhandler

Updated on July 05, 2022

Comments

  • Shimmy Weitzhandler
    Shimmy Weitzhandler almost 2 years

    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?

  • Amir Karimi
    Amir Karimi about 13 years
    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. :(
  • Shimmy Weitzhandler
    Shimmy Weitzhandler about 13 years
    @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.
  • Amir Karimi
    Amir Karimi about 13 years
    Thanks a lot. My mistake was that I was creating the Style in an incorrect location while I was using BasedOn.
  • JoanComasFdz
    JoanComasFdz over 12 years
    What about if a UserControl that will be using that resources is in a Class Library Project and there's no App.xaml?
  • Shimmy Weitzhandler
    Shimmy Weitzhandler over 12 years
    @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.
  • JoanComasFdz
    JoanComasFdz over 12 years
    @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?"
  • Shimmy Weitzhandler
    Shimmy Weitzhandler over 12 years
    Refer to here - applies to Silverlight as well. Add your resources in Generic.xaml.
  • AnjumSKhan
    AnjumSKhan over 8 years
    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.
  • Legends
    Legends over 7 years
    How to reference multiple resource files?
  • Radinator
    Radinator about 7 years
    is there a way to do this without a ResourceDictionary? just loading the external xaml file?