Specifying styles in a WPF user control library
wpf
Solution
Solved. It seems that the technique of using generic.xaml only applies to custom controls, not user controls. Styles defined in generic.xaml (directly, or merged from another assembly) are not accessible by a user controls.
After realising this, I just went back to merging the external assembly resource dictionary within the user control itself, i.e.:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Common;component/MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Not ideal having to do this in each UC, but I can live with it in my scenario as I'll only ever have one or two in these "plugin" projects.
In doing this though, I did uncover another issue, and the following may help someone in the future. The `MyStyles.xaml` file in my "Common" assembly contains no styles of its own - it simply merges a number of other resource dictionaries in that assembly. This was done for convenience, meaning a consumer only needed to merge in MyStyles.xaml, rather than the dozen or so individual XAMLs in that assembly.
It turns out that there is a bug in WPF whereby "nested" merged dictionaries don't get parsed correctly. I found that if I put a style directly in MyStyles.xaml, the user control would find it. However it refuses to recognise any of the styles in the dictionaries that MyStyles.xaml merges! I've now dropped MyStyles.xaml, and have gone back to merging in the individual dictionaries (from within the user control XAML) - everything works, at design-time and runtime.
Problem
I'm having some problems sharing common styles between different projects. Here are my projects:- - "Common" - a class library containing common WPF styles. - "Plugin" - a class library containing a user control (not a custom control). - "Core app" - the core WPF application, which displays the user control defined in "Plugin". "Common" and "Core app" reside in the same solution, while "Plugin" is in a solution of its own. Styles in the "Core app" project work fine - it references the "Common" project and has the following in App.xaml:- ``` <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/Common;component/MyStyles.xaml" /> </ResourceDictionary.MergedDictionaries> ``` When editing XAML files in the "Core app" project, I get intellisense on style names, no squiggly underlines, and everything works fine at runtime too. The Problem is with the "Plugin" project. I've referenced the "Common" assembly, and created a \Themes\Generic.xaml file containing the same merge XAML as above. Generic.xaml has a build action of "Page", and I've added the following to AssemblyInfo.cs:- ``` [assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)] ``` When I edit the XAML of a user control in this "Plugin" project, styles in the "Common" assembly don't show up in intellisense, and VS/Resharper puts a squiggly line under their names. I've even added a style directly to Generic.xaml, but the UC can't see that either. The user control looks something like this:- ``` <UserControl ..blah..> <StackPanel> <TextBlock Style="{StaticResource AStyleInCommonAssembly}" Text="Hello"/> <TextBlock Style="{StaticResource AStyleInGenericXaml}" Text="World"/> </StackPanel> </UserControl> ``` At runtime, WPF does correctly apply styles that reside in the "Common" assembly (I guess it's finding them due to the dictionary merge in App.xaml). However it's still unable to find the style that I added directly to Generic.xaml. What am I missing? Does this approach only work with custom controls, and not user controls as I'm dealing with? My priority is to get things working at runtime, but getting the design-time/intellisense experience would be a bonus.