using System; using System.Windows.Controls; using System.Windows.Media.Animation; using System.Windows; namespace MultiTerm.Wpf.Behaviours; /// /// Smoothens by adding an animation for each change of value. /// Inspired by https://stackoverflow.com/questions/14485818/how-to-update-a-progress-bar-so-it-increases-smoothly /// public class ProgressBarSmoother { public static double GetSmoothValue(DependencyObject obj) { return (double)obj.GetValue(SmoothValueProperty); } public static void SetSmoothValue(DependencyObject obj, double value) { obj.SetValue(SmoothValueProperty, value); } public static readonly DependencyProperty SmoothValueProperty = DependencyProperty.RegisterAttached( "SmoothValue", typeof(double), typeof(ProgressBarSmoother), new PropertyMetadata(0.0, Changing)); private static void Changing(DependencyObject d, DependencyPropertyChangedEventArgs e) { // guard wrong type if(d is not ProgressBar progressBar) { return; } var anim = new DoubleAnimation((double)e.OldValue, (double)e.NewValue, new TimeSpan(0, 0, 0, 0, 250)); progressBar.BeginAnimation(ProgressBar.ValueProperty, anim, HandoffBehavior.Compose); } }