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>
Author by
Shimmy Weitzhandler
Updated on July 05, 2022Comments
-
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 about 13 yearsBut 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 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 about 13 yearsThanks a lot. My mistake was that I was creating the Style in an incorrect location while I was using BasedOn.
-
JoanComasFdz over 12 yearsWhat about if a UserControl that will be using that resources is in a Class Library Project and there's no App.xaml?
-
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 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 over 12 yearsRefer to here - applies to Silverlight as well. Add your resources in Generic.xaml.
-
AnjumSKhan over 8 yearsImportant 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 over 7 yearsHow to reference multiple resource files?
-
Radinator about 7 yearsis there a way to do this without a ResourceDictionary? just loading the external xaml file?