Check the following scenario (others may also apply) [you can create a project, just copy the code in this file in the right file]:
a - Create a ResourceDictionary with the base material (Resources.xaml):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <SolidColorBrush Color="Red" x:Key="Test" /> <Style TargetType="{x:Type GroupBox}" x:Key="Test2" > <Setter Property="Background" Value="Blue" /> </Style> <Style TargetType="{x:Type TextBlock}" > <Setter Property="Foreground" Value="Green" /> </Style> </ResourceDictionary>
b - create a user management base, where others inherit, containing the main resources (UserControlBase.cs):
using System.Windows.Controls; using System; using System.Windows; namespace ResourceTest { public class UserControlBase : UserControl { public UserControlBase() { this.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("ResourceTest;component/Resources.xaml", UriKind.RelativeOrAbsolute) }); } } }
c - Create a UserControl inheriting the database (UserControl1.xaml):
<ResourceTest:UserControlBase x:Class="ResourceTest.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" xmlns:ResourceTest="clr-namespace:ResourceTest" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" > <Grid> <GroupBox BorderBrush="{StaticResource Test}" Margin="3" Header="Test" Style="{DynamicResource Test2}" > <TextBlock Text="TESTTEST" /> </GroupBox> </Grid> </ResourceTest:UserControlBase>
Results : StaticResources are not allowed (and Test BorderBrush is not loaded). DynamicResources are allowed (background is blue), but the designer says that he cannot find the resource in any case (the first time it works fine, but when you open / close the constructor, the resource cannot be resolved). Unnamed resources, such as the TextBlock style, work fine.
Is this a designer's mistake or am I doing something wrong? Is it good to declare resources dynamic in a scenario where resources will never change?

Thanks in advance.
source share