user1416072
user1416072

Reputation: 107

Cannot find static resource in a WPF application when running from another project

I'm new to WPF. I'm using a notification proyect (WPFGrowlingNotifications) that runs just fine, but the problem happens when I reference that project from my main project. I keep getting "Cannot find static resource 'CloseButton'" Here is the CloseButton 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>

Here is how its called:

                <Button x:Name="CloseButton" Grid.Column="1" Width="16" Height="16" HorizontalAlignment="Right" Margin="0,0,12,0" Style="{StaticResource CloseButton}"/>

And this is in the app.xml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\ButtonStyle.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

When running the project by itself there is no problem. I think that when I do a reference from the other proyect, the app.xml is ignored in some way...

Upvotes: 0

Views: 776

Answers (1)

Michael G
Michael G

Reputation: 6745

You need to reference your assembly as the the ResourceDictionary source.

<ResourceDictionary Source="pack://application:,,,/WPFGrowlingNotifications;component/Resources/ButtonStyle.xaml"/>

Note the "WPFGrowlingNotifications" potion of the source string. This needs to be a fully qualified assembly name. You can read more about the Pack URI Syntax here. Specifically, you'll want to read the Resource File Pack URIs - Referenced Assemblies section.

Upvotes: 2

Related Questions