Exception when resource is defined in the control library and is referenced from main app

31 views Asked by At

I have two projects in my solution: main WPF app and control library. Inside control library there are three resource dictionaries: Colors.xaml, Styles.xaml and Generic.xaml with the following contents:

Colors.xaml

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

Styles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}"
           x:Key="DefaultTextBox">
        <Setter Property="Background"
            Value="{StaticResource MyBrush}"></Setter>
    </Style>
</ResourceDictionary>

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Styles.xaml" />
        <ResourceDictionary Source="Color.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now, I want to add this Generic.xaml into application resources and use the textbox style inside main window like this:

App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Class="WpfApplication1.App"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ClassLibrary;component/Generic.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

MainWindow.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="WpfApplication1.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="700" Width="525">
    <TextBox Style="{StaticResource DefaultTextBox}" />
</Window>

BUT the problem is that it leads to exception

'{DependencyProperty.UnsetValue}' is not a valid value for property 'Background'.

At the same time using <TextBox Background="{StaticResource MyBrush}"/> works fine. How can I fix this problem?

0

There are 0 answers