WPF

MaterialDesign Theme 적용 하기

Hask 2022. 9. 13. 15:16

 

private bool isDark;

public bool IsDark
{
    get { return isDark; }
    set
    {
        if (isDark != value)
        {
            isDark = value;
            SetProperty( ref this.isDark, value );
            ThemeHelper.ApplyBase( value );
        }
    }
}

 

public static class ThemeHelper
{
    public static void ApplyBase( bool isDark )
        => ModifyTheme( theme => theme.SetBaseTheme( isDark ? Theme.Dark : Theme.Light ) );

    public static void ApplyPrimary( Swatch swatch )
      => ModifyTheme( theme => theme.SetPrimaryColor( swatch.ExemplarHue.Color ) );

    public static void ApplyAccent( Swatch swatch )
        => ModifyTheme( theme => theme.SetSecondaryColor( swatch.AccentExemplarHue.Color ) );

    public static void ModifyTheme( Action<ITheme> modificationAction )
    {
        PaletteHelper paletteHelper = new PaletteHelper();
        ITheme theme = paletteHelper.GetTheme();
        modificationAction?.Invoke( theme );

        paletteHelper.SetTheme( theme );
    }
}

MVVM을 이용하여 외부에서 호출 적용 해보자.