I'm trying to use Growl Alike WPF Notification in another WCF application. But I'm getting an error when trying to run it. The error that I'm getting is :
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
When I check the inner exception, it says
Cannot find resource named 'CloseButton'. Resource names are case sensitive.
But when I check the ButtonStyle.xaml it has the resource CloseButton.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="NormalBorderBrush" Color="Black" />
<SolidColorBrush x:Key="DefaultedBorderBrush" Color="Black" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA" />
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF" />
<SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD" />
<LinearGradientBrush x:Key="CloseNormal" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#394452" Offset="0.0"/>
<GradientStop Color="#343e4a" Offset="1.0"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="CloseOver" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#515a6b" Offset="0.0"/>
<GradientStop Color="#474f5d" Offset="1.0"/>
</LinearGradientBrush>
<SolidColorBrush x:Key="ClosePressed" Color="#090909" />
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle
Margin="2"
StrokeThickness="1"
Stroke="#60000000"
StrokeDashArray="1 2"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="CloseButton" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="MinHeight" Value="16"/>
<Setter Property="MinWidth" Value="16"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Border x:Name="Border" CornerRadius="3" BorderThickness="0" ClipToBounds="False" Background="{StaticResource CloseNormal}" BorderBrush="{StaticResource NormalBorderBrush}">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" Opacity=".4" BlurRadius="5" Color="Black"/>
</Border.Effect>
<Grid>
<Image Source="pack://application:,,,/Resources/close.png" IsHitTestVisible="False" Margin="2">
<Image.Effect>
<DropShadowEffect Direction="90" ShadowDepth="1" BlurRadius="1"/>
</Image.Effect>
</Image>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource CloseOver}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="Background" Value="{StaticResource ClosePressed}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DefaultedBorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Does any one have a clue what is it that I'm missing??
NOTE :: Source code works fine when run as a individual project
In order to use StaticResource, the style has to be defined before the element you are trying to apply it to within the same visual tree.
You could add your resource dictionary to a control's resources above the button in the visual tree
<Window x:Class="StackOverflow.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 Source="ButtonStyle.xaml"/>
</Window.Resources>
<Grid>
<Button Click="ButtonBase_OnClick" Style="{StaticResource CloseButton}">Click</Button>
</Grid>
or instead you could use DynamicResource, which does not require the style to be at the same level or above in the visual tree.
<Button Click="ButtonBase_OnClick" Style="{DynamicResource CloseButton}">Click</Button>
XAML is cached. VS locks it when you have the *.xaml and *xaml.cs files open. Close them in Visual Studio and you may see the problem go away.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With