1

Not sure what I'm doing wrong here.

I have my Colors defined in a ResourceDictionary : Colors.xaml :

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Color x:Key="Normal">#FF404040</Color>
</ResourceDictionary>

Then used in Brushes.xaml :

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Colors.xaml"/>
        <!-- here I may have more colors-->
    </ResourceDictionary.MergedDictionaries>
    <SolidColorBrush x:Key="Color" Color="{StaticResource Normal}" />
    <!-- here I may have more brushes-->
</ResourceDictionary>

Then Brushes are merged in Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml"/>
        <!-- here I may have more resources-->
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now if I use the Brushes in let say a Border Style like so :

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

I must use it as DynamicResource but if used as StaticResource then I got this error at runtime :

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

I wish to use StaticResource for Brushes everywhere in my app.

lecloneur
  • 424
  • 5
  • 20
  • You should use `Brush` object for background instead of `Color` object, in your example `` (btw, your naming style isn't so good) – Pavel Anikhouski Mar 25 '19 at 11:04
  • yes the naming is not really good, I'm in the process of redoing all that color things. I tried your solution and it's the same error. – lecloneur Mar 25 '19 at 11:07
  • Have a look at this thread https://stackoverflow.com/questions/200839/whats-the-difference-between-staticresource-and-dynamicresource-in-wpf If you didn't set your resource directly from them _App.xaml_, you should use _DynamicResource_. _StaticResource_ is used during object construction – Pavel Anikhouski Mar 25 '19 at 11:15
  • My Generic.xaml is a resource in App.xaml – lecloneur Mar 25 '19 at 11:19

1 Answers1

1

If you want to use StaticResource in your ResourceDictionary, you need to merge the Brushes.xaml ResourceDictionary into the ResourceDictionary where you define the Border style, like you did with Colors.xaml in Brushes.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- "Include" Brushes -->
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="BorderContainer" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="{StaticResource Color}" />
    </Style>
</ResourceDictionary>

For more information on the difference between Static- and DynamicResource see this answer.

Tobias Hoefer
  • 1,058
  • 12
  • 29