change the theme through StyleManager

0 Answers 22 Views
NumericUpDown Window
Dooly
Top achievements
Rank 1
Iron
Dooly asked on 25 Mar 2024, 03:35 AM

Hi,

I am trying to change the theme through telerik StyleManager.Theme.

Not apply :

 <mynumeric:MyNumeric x:Name="TEST" Margin="-2,0,0,0"
                  Value="{Binding ViewModel.SerialNumber,Mode=TwoWay}" 
                   ValueFormat="Numeric"
                   NumberDecimalDigits="0"
                   Minimum="1" Maximum="9999"
                   telerik:StyleManager.Theme="{Binding Path=(mainwindowviewmodel:MainWindowViewModel.CurrentTheme),Converter={StaticResource ThemeToTelerikThemeConverter}}"
                   IsReadOnly="{Binding Path=(mainwindowviewmodel:MainWindowViewModel.IsManagerAccess),Converter={StaticResource InverseBooleanConverter}}"
                   >

Apply :

 <telerik:RadNumericUpDown x:Name="TEST" Margin="-2,0,0,0"
                  Value="{Binding ViewModel.SerialNumber,Mode=TwoWay}" 
                   ValueFormat="Numeric"
                   NumberDecimalDigits="0"
                   Minimum="1" Maximum="9999"
                   telerik:StyleManager.Theme="{Binding Path=(mainwindowviewmodel:MainWindowViewModel.CurrentTheme),Converter={StaticResource ThemeToTelerikThemeConverter}}"
                   IsReadOnly="{Binding Path=(mainwindowviewmodel:MainWindowViewModel.IsManagerAccess),Converter={StaticResource InverseBooleanConverter}}"
                   >

public class MyNumeric : RadNumericUpDown
{
    public override string FormatDisplay()
    {
        if (this.Value.HasValue)
        {
            return this.Value.Value.ToString("0000");
        }
        return base.FormatDisplay();
    }

    public override string FormatEdit()
    {
        if (this.Value.HasValue)
        {
            return this.Value.Value.ToString("0000");
        }
        return base.FormatEdit();
    }
}

internal class ThemeToTelerikThemeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!Enum.IsDefined(typeof(Wpf.Ui.Appearance.ThemeType), value))
            throw new ArgumentException("ExceptionEnumToBooleanConverterValueMustBeAnEnum");

        switch (value)
        {
            case Wpf.Ui.Appearance.ThemeType.Light:
                return "Office2019";
            case Wpf.Ui.Appearance.ThemeType.Dark:
                return "Expression_Dark";

            default:
                return "Office2019";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I'll appreciate any help.

 

Martin Ivanov
Telerik team
commented on 25 Mar 2024, 09:26 AM

I have tried this, but it work on my side. You can check the attached project and see if I am missing anything. Also, you can try to add the following code in your MyNumeric class:

static MyNumeric()
{
	DefaultStyleKeyProperty.OverrideMetadata(typeof(MyNumeric), new FrameworkPropertyMetadata(typeof(MyNumeric)));
}

Dooly
Top achievements
Rank 1
Iron
commented on 26 Mar 2024, 12:21 AM

Thank you for the reply,

I tried this method before, but it didn't work in real time.

I think it only applies the first time.

Perhaps it is created dynamically in Stylemanager and cannot be found in base when accessed.

 

public class MyNumeric : RadNumericUpDown
{
    public override string FormatDisplay()
    {
        if (this.Value.HasValue)
        {
            return this.Value.Value.ToString("0000");
        }
        return base.FormatDisplay();
    }

    public override string FormatEdit()
    {
        if (this.Value.HasValue)
        {
            return this.Value.Value.ToString("0000");
        }
        return base.FormatEdit();
    }

    static MyNumeric()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyNumeric), new FrameworkPropertyMetadata(typeof(MyNumeric)));
    }
}

thank you.

 

Martin Ivanov
Telerik team
commented on 26 Mar 2024, 08:24 AM

StyleManager doesn't support runtime theme change. You can apply the theme only the first time. To support dynamic change of the Telerik theme, you can use the NoXaml dlls and the implicit styles theming.
Dooly
Top achievements
Rank 1
Iron
commented on 26 Mar 2024, 11:33 PM

Thank you for the reply,

I would like to know why one of the following two conditions works 

and the other does not.

1. dynamic apply ok :

<telerik:RadNumericUpDown x:Name="TEST" Margin="-2,0,0,0"
...
...

2. dynamic not apply : 

<mynumeric:MyNumeric x:Name="TEST" Margin="-2,0,0,0"
...
...

Martin Ivanov
Telerik team
commented on 27 Mar 2024, 02:00 PM

This doesn't work because you will need to replace the DefaultStyleKey on theme change. You can use something like this:

public class MyNumeric : RadNumericUpDown
{
    public void ResetTheme()
    {
        Theme themeToApply = StyleManager.GetTheme(this);
        DefaultStyleKey = ThemeResourceKey.GetDefaultStyleKey(themeToApply, typeof(RadNumericUpDown));
    }
}

//-----------------

CurrentTheme = newThemeNameHere;
this.myNumericUpDown.ResetTheme();

However, I still strongly recommend you to not rely on the StyleManager for runtime theme changes. It works partially, but there could be glitches or other unsupported scenarios. 

Dooly
Top achievements
Rank 1
Iron
commented on 27 Mar 2024, 11:42 PM

Hi Martin,

Thank you, this worked perfectly!

The reason for using StyleManager was to simply modify a project using another library by

adding some telerik controls.

Thank you so much, 

 

No answers yet. Maybe you can help?

Tags
NumericUpDown Window
Asked by
Dooly
Top achievements
Rank 1
Iron
Share this question
or