I’m having a problem with WPF resources accessing external style info in an external ResourceDictionary through StaticResources.
I have inherited a bunch of code that uses a lot of DynamicResources in their WPF to get style information from a Shared.xaml. I would rather them be StaticResources so that the designer view becomes useful. I discovered that I can add a ResourceDictionary in the .Resources of the thing, which works fine UNLESS there is already a .Resources using in the WPF. In that case I am told I need to add a x:Key to my ResourceDictionary. I do that except that now I don’t know how to use that key to find the resources statically.
Minimal example:
<UserControl x:Class="myClass"
...>
<UserControl.Resources>
<ResourceDictionary x:Key="Shared.xaml" Source="/ExteralResource;component/Shared.xaml"/>
<BooleanToVisibilityConverst x:Key="BoolToVis">
</UserControl.Resources>
<TextBlock Background="{StaticResource BrushFromSharedXAML}" /> <!-- Never finds BrushSharedXAML -->
</UserControl>
Any ideas?
Though, be aware that doing this in each usercontrol is going to eat a lot of RAM, because you’re actually creating a new instance of the Shared.xaml ResourceDictionary for every instance of this control at runtime. You should take a look at SharedResourceDictionary