Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a WPF style from another assembly

Tags:

c#

wpf

xaml

I have been googling for hours trying out different solutions to my problem but have not been able to find one there or on here.

I have a WPF Custom Control Library that also includes a theme .xaml file that has styles I want to apply to controls, however after linking it as a ResourceDictionary I am unable to access the style when modifying the style attribute of a control.

This is how I am linking it

<ResourceDictionary Source="pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml"/>

and this is the .xaml file content:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:MADEV.WPFNotification">

<SolidColorBrush  x:Key="BG"
                  Color="#FF464646" />
<SolidColorBrush x:Key="BG_Darker"
                 Color="#FF3A3A3A" />
<SolidColorBrush x:Key="FG"
                 Color="#FFC5C5C5" />

<Style x:Key="NotificationStyle"
       TargetType="Window">
    <Setter Property="Height"
            Value="36" />
    <Setter Property="Width"
            Value="150" />
    <Setter Property="ResizeMode"
            Value="NoResize" />
    <Setter Property="ShowInTaskbar"
            Value="False" />
    <Setter Property="Topmost"
            Value="True" />
    <Setter Property="Focusable"
            Value="False" />
    <Setter Property="IsTabStop"
            Value="False" />
    <Setter Property="WindowStyle"
            Value="None" />
    <Setter Property="Foreground"
            Value="White" />
    <Setter Property="Background"
            Value="{StaticResource BG}" />
    <Setter Property="AllowsTransparency"
            Value="True" />
    <Setter Property="IsHitTestVisible"
            Value="False" />
    <Setter Property="ShowActivated"
            Value="False" />
</Style>

I would appreciate some help with this

Edit 1: Current App.xaml after vesan's answer, yet still not working:

<Application x:Class="SimpleOSD.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:SimpleOSD"
         xmlns:properties="clr-namespace:SimpleOSD.Properties"
         StartupUri="BackgroundProcess.xaml">
<Application.Resources>
    <properties:Settings x:Key="Settings" />

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Pack://application:,,,/MADEV.WPFNotification;component/Themes/Dark.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

like image 743
Mitchell Van Manen Avatar asked Aug 24 '15 19:08

Mitchell Van Manen


1 Answers

OK, I'll just post the most basic implementation, which will hopefully show you the right direction.

First the control library, project WpfControlLibrary1, file Dictionary1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="TestBrush" Color="LightBlue"></SolidColorBrush>
</ResourceDictionary>

Now the WPF application, WpfApplication1 (references the control library), file App.xaml:

<Application x:Class="WpfApplication1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Dictionary1.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

And finally, Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="400" Width="600" 
        Background="{StaticResource TestBrush}">
</Window>

Referencing the resource dictionary in App.xaml will make it available to all windows/controls in your WPF application. If you don't want that, you can move the code from App.xaml to a specific XAML file.

And here's the result:

enter image description here

like image 113
vesan Avatar answered Oct 04 '22 05:10

vesan