Associated Resource Dictionary and StaticResource Root Link

I know this , which is not relevant to my case, and this , which I am not sure whether it can be adapted.

I am working on a WPF management library and I do not have an App.xaml file. I am using a file called Styles.xml to store shared brushes and other resources. In the XAML file of my custom element, I import resources, and then I try to use the sBrush brush as the background.

This works, except at the root level:

<UserControl x:Class="CMControls.TitledWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" Background="{StaticResource ResourceKey=sBrush}"> <!--EXCEPTION!--> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/CMControls;component/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <Canvas Background="{StaticResource ResourceKey=sBrush}" ... <!--Ok.--> ... 

I assume this happens because when an instance of the root element is created, its children are not included, including UserControl.Resources . Is there a workaround? Please note that everything works fine in the designer, no matter where I make the link .

+4
source share
1 answer

Change the UserControl Background after pooling the resources, because you need to add resources before using them!

  <UserControl x:Class="CMControls.TitledWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/CMControls;component/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </UserControl.Resources> <UserControl.Background> <!--Set background here!--> <StaticResource ResourceKey="sBrush"></StaticResource> </UserControl.Background> ... 
+4
source

Source: https://habr.com/ru/post/1488237/


All Articles