Click here to Skip to main content
15,844,152 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two different projects...

A WPF Control Library project where, among other files, I have some Resource Dictionary files...

And a WPF Application project where into it's references I have add my WPF Control Library.

My problem is when I try to apply a style on a Window element from that external resource dictionary file.

Style's changes like, Background become visible only in running mode!!!

What I have tried:

Let's say this is my Window.xaml style file which is located into my WPF Control Library project:
XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="Window_Style" TargetType="Window">
        <Setter Property="Background" Value="#FF272727"/>
    </Style>

</ResourceDictionary>

First I add it as reference into my WPF Application project and then I add it into App.xaml file of my WPF Application project:
XML
<Application x:Class="MyLibMaster.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyLibMaster"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/MyLib;component/Resources/Styles/Window.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

Then I apply it to my Window like this:
XML
<Window x:Class="MyLibMaster.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:MyLibMaster"
        mc:Ignorable="d"
        Title="MainWindow" Height="480" Width="840"
        
        Style="{DynamicResource Window_Style}">

    <Grid >
        
    </Grid>
    
</Window>
Posted
Updated 18-Feb-23 3:43am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900