Reference to the parent Resource in UserControl

I have a WPF library UserControls and a ResourceDictionary that are shared in a library.

All UserControls in this library appear in only one parent "shell" element, which is actually just a container for a collection of smaller controls. I can access the ResourceDictionary from my shell control, as expected, when I add the following XAML

<Control.Resources> <ResourceDictionary Source="MyResources.xaml" /> </Control.Resources> 

However, I cannot access the ResourceDictionary from the child controls that are inside the shell control.

I got the impression that WPF should check the resources locally and then move up until the appropriate resources are found?

Instead i get

 Cannot find resource named '{BoolInverterConverter}'. Resource names are case sensitive. Error at object 'System.Windows.Data.Binding' in markup file... 

Obviously, I can (and I) refer to the ResourceDictionary in my child controls; but each control should now reference this dictionary, and I figured this was optional.

Any ideas, am I doing something weird or don't believe in the wrong behavior?

+4
source share
1 answer

What happens is described here , although the documentation is a bit opaque. If you add a ResourceDictionary to the element’s Resources property without specifying a key, WPF expects you to go into the resource dictionaries and it populates the dictionary with the contents of the dictionaries in the MergedDictionaries property. It ignores the actual contents of the ResourceDictionary without a key.

So what do you want to do:

 <Control.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="MyResources.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Control.Resources> 

Edit:

Working example:

MainWindow.xaml:

 <Window x:Class="MergedDictionariesDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:MergedDictionariesDemo="clr-namespace:MergedDictionariesDemo" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary1.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <MergedDictionariesDemo:UserControl1 /> </Grid> </Window> 

Dictionary1.xaml:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush x:Key="UCBrush" Color="Bisque" /> </ResourceDictionary> 

UserControl1.xaml:

 <UserControl x:Class="MergedDictionariesDemo.UserControl1" 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"> <Border Margin="10" BorderBrush="Navy" BorderThickness="1" CornerRadius="10"> <TextBlock Margin="10" Background="{DynamicResource UCBrush}"> The background of this is set by the resource UCBrush. </TextBlock> </Border> </UserControl> 
+4
source

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


All Articles