Search code examples
c#wpfxamlmaterial-design-in-xaml

Globally accessible style in class library


I have a class library where I'm defining (basically extending) some controls such as TextBox, Button etc. I'm also using MaterialDesignInXamlToolkit which is used to stylize controls. So my class library will essentially have controls with my own extended functionality and they will look like styles defined in MaterialDesignInXamlToolkit.

Now my question is, since I don't have App.xaml in class library project, where should I write the XAML code to import the styles of MaterialDesignInXamlToolkit, so that they will be applied to my extended controls? What is the place in class library where you can specify styles which are globally accessible and are applied to all the controls?

I searched about this but didn't find what I want. Please help.


Update: Here is my code (not working).

MaterialTextBox.cs

using System.Windows.Controls;

namespace MaterialControls
{
    public class MaterialTextBox : TextBox
    {
        ... some extra features here (no XAML file for this class, just this .cs)...
    }
}

Themes.xaml (this will contain all the global styles)

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

        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />

        <ResourceDictionary>
            <Style TargetType="local:MaterialTextBox">
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Height" Value="100"/>
            </Style>
        </ResourceDictionary>

    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Now I want these styles to apply to MaterialTextBox so that wherever I use it, it should come with this look and featues out of the box.


Solution

  • Found the solution.

    I was using Class Library project where I actually should have used WPF Custom Control Library project. Here project type is important otherwise you will have to play with .csproj file to make it work.

    So now created a new WPF Custom Control Library project (New Project > Windows > Classic Desktop > WPF Custom Control Library template). This project has Themes\Generic.xaml file which will be used as a default location for styles.