Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow Inherit to be specified for the CustomColorTheme as well
This allows for using the system theme accent color when using the CustomColorTheme.
  • Loading branch information
Keboo committed Mar 14, 2025
commit fc8a2299eca4885fba17500f7ca194319ec7bd11
8 changes: 6 additions & 2 deletions src/MainDemo.Wpf/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This is the current way to set up your app's initial theme -->
<!-- PrimaryColor and SecondaryColor also support the constant string "Inherit" to specify the color should use the system theme accent color -->
<materialDesign:BundledTheme BaseTheme="Inherit"
ColorAdjustment="{materialDesign:ColorAdjustment}"
PrimaryColor="DeepPurple"
SecondaryColor="Lime" />

<!-- If you would prefer to use your own colors, there is an option for that as well: -->
<!--<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="DarkGreen" />-->
<!--
If you would prefer to use your own colors, there is an option for that as well:
PrimaryColor and SecondaryColor also support the constant string "Inherit" to specify the color should use the system theme accent color
-->
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="Aqua" SecondaryColor="#FF006400" />

<!-- You can also use the built-in theme dictionaries: -->
<!--
Expand Down
6 changes: 3 additions & 3 deletions src/MaterialDesignColors.Wpf/SwatchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace MaterialDesignColors;

public static class SwatchHelper
{
public static IEnumerable<ISwatch> Swatches { get; } = new ISwatch[]
{
public static IEnumerable<ISwatch> Swatches { get; } =
[
new RedSwatch(),
new PinkSwatch(),
new PurpleSwatch(),
Expand All @@ -26,7 +26,7 @@ public static class SwatchHelper
new BrownSwatch(),
new GreySwatch(),
new BlueGreySwatch(),
};
];

public static IDictionary<MaterialDesignColor, Color> Lookup { get; } = Swatches.SelectMany(o => o.Lookup).ToDictionary(o => o.Key, o => o.Value);
}
4 changes: 2 additions & 2 deletions src/MaterialDesignThemes.Wpf/BundledTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ PrimaryColor is not PrimaryColor primaryColor ||
Lazy<Color?> accentColor = new(Theme.GetSystemAccentColor);

Color colorPrimary = primaryColor == MaterialDesignColors.PrimaryColor.Inherit
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
? (accentColor.Value ?? default)
: SwatchHelper.Lookup[(MaterialDesignColor)primaryColor];

Color colorSecondary = secondaryColor == MaterialDesignColors.SecondaryColor.Inherit
? (accentColor.Value ?? SwatchHelper.Lookup.First().Value)
? (accentColor.Value ?? default)
: SwatchHelper.Lookup[(MaterialDesignColor)secondaryColor];

Theme theme = Theme.Create(baseTheme, colorPrimary, colorSecondary);
Expand Down
5 changes: 4 additions & 1 deletion src/MaterialDesignThemes.Wpf/CustomColorTheme.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows.Media;
using System.ComponentModel;
using System.Windows.Media;

namespace MaterialDesignThemes.Wpf;

Expand All @@ -19,6 +20,7 @@ public BaseTheme? BaseTheme
}

private Color? _primaryColor;
[TypeConverter(typeof(InheritSystemColorTypeConverter))]
public Color? PrimaryColor
{
get => _primaryColor;
Expand All @@ -33,6 +35,7 @@ public Color? PrimaryColor
}

private Color? _secondaryColor;
[TypeConverter(typeof(InheritSystemColorTypeConverter))]
public Color? SecondaryColor
{
get => _secondaryColor;
Expand Down
49 changes: 49 additions & 0 deletions src/MaterialDesignThemes.Wpf/InheritSystemColorTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Media;

namespace MaterialDesignThemes.Wpf;

internal sealed class InheritSystemColorTypeConverter : TypeConverter
{
private const string Inherit = "Inherit";

private ColorConverter ColorConverter { get; } = new();

public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
=> sourceType == typeof(string) ||
ColorConverter.CanConvertFrom(context, sourceType) ||
base.CanConvertFrom(context, sourceType);

public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
=> ColorConverter.CanConvertTo(context, destinationType) ||
base.CanConvertTo(context, destinationType);

public override object ConvertFrom(ITypeDescriptorContext? td, System.Globalization.CultureInfo? ci, object? value)
{
if (value is null)
{
throw GetConvertFromException(value);
}

string? s = value as string ?? throw new ArgumentNullException(nameof(value));

if (string.Equals(s, Inherit, StringComparison.OrdinalIgnoreCase))
{
return Theme.GetSystemAccentColor() ?? default;
}

return ColorConverter.ConvertFrom(td, ci, s);
}

public override object ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType)
{
if (value is Color color &&
color != default &&
color == Theme.GetSystemAccentColor())
{
return Inherit;
}
return ColorConverter.ConvertTo(context, culture, value, destinationType);
}
}
Loading