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!!!