The problem is because of incorrect order of your styles. I mean that if we publish button style with reference on static resource NormalBrush
, which is declared under the style, like this:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{StaticResource NormalBrush}" >
<ContentPresenter Name="ButtonCont"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#CCC" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
This code will give you such error. But if you change, like this
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#FFF" Offset="0.0"/>
<GradientStop Color="#CCC" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{StaticResource ButtonBackground}" >
<ContentPresenter Name="ButtonCont"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Everything works fine. The same this occurs if your resources are stored in different xaml files and you attach them in your ResourceDictionary
. For example (consider we store our NormalBrush
in Colors.xaml
and Button
in Button.xaml
). This gives error:
<ResourceDictionary Source="Resources\Button.xaml" />
<ResourceDictionary Source="Resources\Colors.xaml" />
This should work error:
<ResourceDictionary Source="Resources\Colors.xaml" />
<ResourceDictionary Source="Resources\Button.xaml" />