Mike143
Mike143

Reputation: 163

Set resource or style to a window from different class library

I am following prism to build wpf application. In my application, there is a helper class library where I defined resources and styles which is going to be used by multiple class libraries in the solution.

But I am getting error when I try to set a resource to Style attribute of a window. The window is in differnt class library as of a resource.

The error that I am getting is : Cannot find resource named '{resourceName}'. Resource names are case sensitive. Error at object 'MyWindow' in markup file

I used the following code but it is not working

<Window x:Class="Class1" Style="{StaticResource resourceName}">

<Window.Resources>    
        <ResourceDictionary>     
        <ResourceDictionary.MergedDictionaries>

       <ResourceDictionary Source="pack://application:,,,/classlibrary;component/Dictionary/resource1.xaml" />              </ResourceDictionary.MergedDictionaries>

    < /ResourceDictionary>

< /Window.Resources>
</Window>

Upvotes: 2

Views: 4076

Answers (2)

David Veeneman
David Veeneman

Reputation: 19132

You can use a pack URL to get a resource dictionary from another assembly. Check this thread.

Here is an example of a resource dictionary read from a Common assembly:

<Application.Resources> 
    <ResourceDictionary> 
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="pack://application:,,,/Common;component/styles.xaml"/> 
        </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources>

Upvotes: 1

dugas
dugas

Reputation: 12493

You need to use the DynamicResource markup extension. The reason for this is because the external ResourceDictionary you import is declared in your xaml after the style is set for your Window. A working example of setting a Window's style with a style defined in a referenced class library is below:

The "MainWindow" xaml:

<Window x:Class="WpfApplication3.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"
    Style="{DynamicResource TestStyle}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary 
Source="pack://application:,,,/ClassLibrary1;component/Styles.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>

The ResourceDictionary (defined in a file named Styles.xaml) in a class library:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="TestStyle" TargetType="Window">
    <Setter Property="Background">
        <Setter.Value>
            <SolidColorBrush Color="Green"></SolidColorBrush>
        </Setter.Value>
    </Setter>
</Style>

</ResourceDictionary>

Upvotes: 3

Related Questions