How to apply custom color as controller background in MaterialDesign in WPF

56 Views Asked by At

I've created my own Colors.xaml file as follows:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
    
    <Color x:Key="CorePurple1">#a100ff</Color>
    <Color x:Key="CorePurple2">#7500c0</Color>
    <Color x:Key="CorePurple3">#460073</Color>
    <Color x:Key="Purple1">#b455aa</Color>

    <SolidColorBrush x:Key="CorePurple1Brush" Color="{StaticResource CorePurple1}"/>
    <SolidColorBrush x:Key="CorePurple2Brush" Color="{StaticResource CorePurple2}"/>
    <SolidColorBrush x:Key="CorePurple3Brush" Color="{StaticResource CorePurple3}"/>
    <SolidColorBrush x:Key="Purple1Brush" Color="{StaticResource Purple1}"/>

    <!--PRIMARY-->
    <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource CorePurple1}"/>
    <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="White"/>
    <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource CorePurple2}"/>
    <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="White"/>
    <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource CorePurple3}"/>
    <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="Black"/>
    <SolidColorBrush x:Key="MaterialDesignBackground" Color="{StaticResource Purple1}"/>

</ResourceDictionary>

Then in the App.xaml file, I refer MaterialDesignTheme as instructed:

<Application x:Class="Automation.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="{StaticResource CorePurple3}" SecondaryColor="{StaticResource Purple1}" />-->
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                <ResourceDictionary Source="/Resources/Colors.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Now when I launch the program, the background color of controllers, such as GroupBox and ListBox, is not what I want but total black. The background color of button however is as what I have set. I think the problem lies in the key "MaterialDesignBackground", but have no clue?

Also, when I add the line <materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="{StaticResource CorePurple3}" SecondaryColor="{StaticResource Purple1}" />, an exception pops up when launching the program, saying that "CorePurple3 not found".

Up to now I have tried to move the MaterialDesignBackgroud value outside the custom xaml file and moved it to the App.xaml, but it never works. Also if I commont the line <materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="{StaticResource CorePurple3}" SecondaryColor="{StaticResource Purple1}" />, the background color is still black.

How can I correctly set the custom background color in the MaterialDesignTheme?

0

There are 0 best solutions below