How to change WPF Material Design Palette at runtime?

material design wpf
wpf material design textbox style
wpf change theme at runtime
wpf material design dark theme
material design color palette
material design tab control wpf
material design icons wpf
wpf material design menu

I've recently "upgraded" a solution to a VS2017 and .NET Framework 4.6.1.

I've also updated all of the NuGet packages to their latest versions.

This question likely concerns one or more of the following packages (previous versions are in parenthesizes):

MahApps.Metro by Jan Karger et al. 1.6.5 (1.3.0-ALPHA016)

MaterialDesignColors by James Willock 1.1.3 (1.1.2)

MaterialDesignThemes by James Willock 2.5.0.1205 (1.1.0.234)

MaterialDesignThemes.MahApps by James Willock 0.0.12 (0.0.3)

WPF related packages also being referenced include:

Extended.Wpf.Toolkit by Xceed 3.4.0 (2.6.0)

ControlzEx by Jan Karger et al. 3.0.2.4 (none, new dependency of MahApps.Metro)

Prior to the updates, the following would change the UI color palette at runtime:

private void primaryPaletteComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
   paletteHelper.ReplacePrimaryColor(this.primaryPaletteComboBox.Text);
}

/* Where: */

var paletteHelper = new MaterialDesignThemes.Wpf.PaletteHelper();

this.primaryPaletteComboBox.Items.AddRange(new object[] {
    "Amber",
    "Blue",
    "BlueGrey",
    "Brown",
    "Cyan",
    "DeepOrange",
    "DeepPurple",
    "Green",
    "Grey",
    "Indigo",
    "LightBlue",
    "LightGreen",
    "Lime",
    "Orange",
    "Pink",
    "Purple",
    "Red",
    "Teal",
    "Yellow"});

...Super simple, super easy.

After the updates, ReplacePrimaryColor throws the following exception:

System.InvalidOperationException: 'Unable to safely determine a single resource definition for SecondaryAccentBrush.'

Downgrading these packages to their previous versions cause other problems.

How can I change the color palette at runtime in the most recent version of these packages?

I would like to do this by simply using the color palette name that the user selects from a ComboBox.

Obviously SecondaryAccentBrush is a problem.

Does anyone know what changed in the last few years?

This should be easy, but Google doesn't give me anything useful. Maybe I'm not asking the right question.

PaletteHelper now provides a ReplacePalette(Palette palette) method that looks promising, perhaps there is a way to instantiate a Palette object by using the name of a predefined resource?

I would rather go to the dentist and proctologist at the same time, than to screw around with XAML resource definitions.


This is my work around, it's not the answer.

So at startup, the default palette is set in App.xaml:

<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.SWATCHNAME.xaml" />

What I'm doing to change it at runtime is:

Uri uri = new Uri($"pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.{SWATCHNAME}.xaml");
System.Windows.Application.Current.Resources.MergedDictionaries.RemoveAt(4);
System.Windows.Application.Current.Resources.MergedDictionaries.Insert(4, new ResourceDictionary() { Source = uri });

This does what I want in the sense that I can specify a single color swatch, and not have to specify an accent, or set the Hue indices, in the Palette constructor.

The resource in question is always at index 4, so for now I'm going with it as no keys are defined in App.xaml.

The only other caveat is only some elements are changed at runtime. The app has to be restarted before all of the elements get the new colors, by using the same lines of code in AppStart.cs.

I hope this helps someone else. (note the SWATCHNAME placeholder, and your specific resource index)

If anyone has a better idea, lay it on me.

C# WPF Design UI: Material Design Custom Colors, How to set Theme and Color in your XAML Before to start, here is a quick reminder about MAterial Design Theme and Colors. The Primary and Secondary Colors will be used to change all colors 3.1 Theme Dynamically. WPF with Material Design use Theme and Accent Palette to change in runtime. How can I save it?


Question is quite old but maybe it will help someone else. I think you are missing some entries in your xaml dictionary (related to Accent colors). Minimum should be similar to following

<Application x:Class="MaterialTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <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 Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />

            </ResourceDictionary.MergedDictionaries>            
        </ResourceDictionary>
    </Application.Resources>
</Application>

But this is not enough in your case. You are integrating with MahApp.Metro Please follow this link to find more about integration: https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/MahApps.Metro-integration

Seems like you have your themes hardcoded. It is better to get list of available themes directly from library. Then you can use this like that:

SwatchesProvider swatchesProvider = new SwatchesProvider();
List<string> PrimaryColorsList = swatchesProvider.Swatches.Select(a => a.Name).ToList();
this.primaryPaletteComboBox.Items.AddRange(PrimaryColorsList);

private void primaryPaletteComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    SwatchesProvider swatchesProvider = new SwatchesProvider();
    Swatch color= swatchesProvider.Swatches.FirstOrDefault(a => a.Name == this.primaryPaletteComboBox.Text);
    paletteHelper.ReplacePrimaryColor(color);
}

Material Design theme Manager -First Post, Switching Themes at Design-time Before proceeding with this tutorial, please first read the Setting a Theme By utilizing the theming mechanism with implicit styles, you can change the theme of Telerik UI for WPF controls at runtime without Figure 2: The Windows8 theme shows a different set of colors for all controls  I have a WPF application with a theme (ShinyRed.xaml) and I want to have a button that when clicked changes the theme to ShinyBlue.xaml I load in the theme initially in App.xaml: &lt;Application.


You can achieve this using MDIX PaletteHelper.

For Dark/Light

Drop a ToggleButton on your Ui & bind its isChecked bool to the isDark bool used below (Binding or Code behind, however you are wiring your ui up)

    private readonly PaletteHelper _paletteHelper = new PaletteHelper();
    private void ToggleBaseColour(bool isDark)
    {
        ITheme theme = _paletteHelper.GetTheme();
        IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
        theme.SetBaseTheme(baseTheme);
        _paletteHelper.SetTheme(theme);
    }

For Primary & Accent colours

the MDIX GitHub demo apps PaletteSelector VM has well written & easy to read code here

Switching Themes at Runtime, WPF with Material Design use Theme and Accent Palette to change in runtime. How can I save it? How to change WPF Material Design Palette at runtime? I've recently "upgraded" a solution to a VS2017 and .NET Framework 4.6.1. I've also updated all of the NuGet packages to their latest versions.


How to save Theme, Accent in Settings WPF w Material Design , How can I change the accent color directly from a button for exemple (In VB. UP :) Is there a way to change Material Design accent in VB. resources so they can change at runtimebut I don't have an example right now. and other recent changes, I've updated your code-behind and app.xaml, as per  Welcome to the Material Design In XAML Toolkit Wiki! Start at one of these places: Super Quick Start. Or, the more comprehensive Getting Started guide.. Quick start guide for using icons: Using Icons


Changing Accent color in VB.Net · Issue #36 , With Material Design In XAML Toolkit you can easily bring beautiful desktop applications to life, Easy configuration of Material Design Colour palettes at both design and run time; Transitions API Start a new project, and install from NuGet. Style and Appearance / Themes Suite. Material Theme. With the R2 2017 release of the UI for WPF suite we have introduced the brand new Material Theme.It comes with built-in elements that provide interaction feedback, with easy-to-customize colors and Material design drop shadows with beautiful and smooth transitions between the states.


Material Design In XAML, WPF with Material Design use Theme and Accent Palette to change in runtime. How can I save it in Settings like LEFT,TOP, HEIGHT, WIDTH  Material Design styles for all major WPF Framework controls Additional controls to support the theme, including Multi Action Button, Cards, Dialogs, Clock Easy configuration of palette (at design and runtime), according to Google's guidelines