Reputation: 1759
I have created a simple project that demonstrates the issue I am having where I get the error: 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '6' and line position '9'.
The project layout is very simple and I have uploaded it to dropbox: https://www.dropbox.com/s/451b5zkw8oqgcld/StyleTest1.zip?dl=0
MainWindow.xaml
<Window x:Class="StyleTest1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
<ResourceDictionary Source="Dictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}">
</Button>
</Grid>
</Window>
Dictionary1.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<GradientStopCollection po:Freeze="true" x:Key="ButtonBackgroundStops">
<GradientStop Color="#2d2d2f"/>
<GradientStop Color="#2d2d2f" Offset="1"/>
</GradientStopCollection>
<LinearGradientBrush
po:Freeze="true"
x:Key="ButtonBackgroundBrush"
GradientStops="{StaticResource ButtonBackgroundStops}"
StartPoint="0.5,-0.05"
EndPoint="0.5,0.66" />
</ResourceDictionary>
Dictionary2.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush
x:Key="Button.Static.Background"
GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}"
StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}"
EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/>
</ResourceDictionary>
And that's it... if I run that program I get the error: 'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '6' and line position '9'.
However, if I alter MainWindow.xaml to the following I no longer get the issue: Here is the dropbox link for the modified version: https://www.dropbox.com/s/ceikh5b8cfecdkw/StyleTest2.zip?dl=0
MainWindow.xaml
<Window x:Class="StyleTest2.MainWindow"
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"
xmlns:local="clr-namespace:StyleTest2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
<ResourceDictionary Source="Dictionary2.xaml" />
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush
x:Key="Button.Static.Background"
GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}"
StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" />
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}">
</Button>
</Grid>
</Window>
Which would suggest it is a problem with the LinearGradientBrush in Dictionary2.xaml binding to the ButtonBackgroundBrush resource that is located in Dictionary1.xaml.
Can anyone tell me what I am doing wrong here and what is the correct way to have a resource in one dictionary reference a resource in another dictionary?
Thanks for your time,
codeOwl
Upvotes: 0
Views: 1642
Reputation: 9827
Use DynamicResource
in-place of StaticResource
in Dictionary2,
or
Merge Dictionary 1 in Dictionary2, then there won't be any issues.
Dictionary2 will look like :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
<LinearGradientBrush
x:Key="Button.Static.Background"
GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}"
StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}"
EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/>
</ResourceDictionary>
Upvotes: 3