Completed
Last Updated: 11 Mar 2024 07:38 by ADMIN
Release 2024.1.228 (Preview)
Martin Ivanov
Created on: 16 Feb 2024 12:07
Category: Buttons
Type: Bug Report
1
Buttons: Initial switch of the RadToggleSwitchButton's thumb is delayed when the animation is disabled

RadToggleSwitchButton: The animation of the thumb switch updates its position when the animation finishes. This happens with a slight delay. However, if the animation is disabled, this delay should not be executed. Instead, the thumb should be updated immediately.
Currently, the delay can causes an animation-like effect in some specific situations.

To work this around, you can create a custom RadToggleSwitchButton and override the property changed callback of the IsChecked property.

public class CustomToggleSwitchButton : RadToggleSwitchButton
{
    private FrameworkElement thumb;

    static CustomToggleSwitchButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomToggleSwitchButton), new FrameworkPropertyMetadata(typeof(CustomToggleSwitchButton)));
        IsCheckedProperty.OverrideMetadata(typeof(CustomToggleSwitchButton), new FrameworkPropertyMetadata(OnIsCheckedPropertyChanged));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.thumb = this.GetTemplateChild("PART_Thumb") as FrameworkElement;
    }

    private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var switchButton = (CustomToggleSwitchButton)d;

        if (switchButton.thumb == null)
        {
            return;
        }

        bool? isChecked = e.NewValue as bool?;
        if (isChecked == true)
        {
            switchButton.thumb.HorizontalAlignment = HorizontalAlignment.Right;                
        }
        else if (isChecked == false)
        {
            switchButton.thumb.HorizontalAlignment = HorizontalAlignment.Left;
        }
        else if (switchButton.IsThreeState)
        {
            switchButton.thumb.HorizontalAlignment = HorizontalAlignment.Center;
        }
    }
}

0 comments