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.
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))); }
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.
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" ... ...
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.
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,