Styles of Window element aren't apply in Design Mode when they are coming from an external Resource Dictionary included in .dll

Simos Sigma 1 Reputation point
2023-02-19T13:44:47.7233333+00:00

As the title says, I am facing an issue when styles of a Window element are coming from an external Resource Dictionary which is included into a .dll file. The problem is that they aren't apply in Design Mode!!!

To be more specific...

I have make a WPF Control Library project which, among other stuff, includes some Resource Dictionary (.xaml) files with styles. To use these styles to a WPF Application project I can use two ways...

WAY ONE: ( Example File )
■ Into the Solustion of my Application project I add my Control Library as a new project.
■ I go to my Application project and I add a reference that points to my Control Library project**.** Please pay attention, a PROJECT REFERENCE, not a .dll reference.
■ I add the .xaml file from my Control Library project into the App.xaml file of my Application project as merged dictionaries.
■ And finally I give the desired style to my Window element.

Using way one it works fine, in Design Mode too, but what if, for various obvious reasons, I want to give my Control Library to someone as a .dll file? Then I (think) I have to use the second way...

WAY TWO: ( Example File )
Without to add my Control Library as a new project into the Solustion of my Application project, I go to my Application project and I add a reference that points to the .dll file of my Control Library project**.** Please pay attention, a DLL REFERENCE, not a project reference.
■ I add the .xaml file from my Control Library project into the App.xaml file of my Application project as merged dictionaries.
■ And finally I give the desired style to my Window element.

Using this way, styles are applying only when I run the program and not in Design Mode!!! And this happens only for Window elements!!! As you can see into the code of "Way Two" example, the Grid gets the given style even in Design Mode. The problem is with the Window element!!!

In case you don't want to check my example files, here is an example code...

This is my Window.xaml file which is located into my WPF Control Library project:

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

    <Style x:Key="MainWindow_Style" TargetType="{x:Type Window}">
        <Setter Property="Background" Value="Green"/>
    </Style>

    <Style x:Key="Grid_Style" TargetType="{x:Type Grid}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Margin" Value="100"/>
    </Style>

</ResourceDictionary>

This is my App.xaml which is located into my WPF Application project:

<Application x:Class="MyApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyApp"
             StartupUri="MainWindow.xaml">

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

</Application>

And this is how I apply styles on the elements:

<Window x:Class="MyApp.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:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        Style="{DynamicResource MainWindow_Style}">
    
    <Grid Style="{DynamicResource Grid_Style}">
        
    </Grid>
    
</Window>

PS: My whole project is a .NET 4.8 Framework project!!!

Developer technologies | Windows Presentation Foundation
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | Visual Studio | Other
Developer technologies | XAML
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-02-20T05:41:50.9733333+00:00

    What is the structure of your project? I'm working on your code in my project and it works fine.

    project structure:

    User's image

        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/WpfCustomControlLibrary1;component/Resources/Styles/Dictionary1.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    

    The result: enter image description here


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Limitless Technology 44,766 Reputation points
    2023-02-20T12:47:23.5233333+00:00

    Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query

    If you are trying to apply Window style properties from an external resource dictionary (DLL) in design mode but they are not being applied, it is likely because the resource dictionary is not being loaded properly. To resolve this issue, you will need to make sure that the DLL is being loaded correctly. You can do this by adding the DLL as an application resource, and then referencing the DLL in the MergedDictionaries collection of the Window. You can also use the Source property of the Window to manually reference the DLL, but this is not recommended. Once the DLL is loaded correctly, the Window style properties should be applied in design mode.

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.