Created
October 23, 2021 08:07
-
-
Save AngryCarrot789/b0e88ce0f4de16db4e5e850c9ac715e4 to your computer and use it in GitHub Desktop.
Slider set value to a default value using middle mouse button click
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Input; | |
using System.Windows.Media.Animation; | |
namespace ControlsLib.AttachedProperties { | |
public static class SliderDefaultValues { | |
public static readonly DependencyProperty IsEnabledProperty = | |
DependencyProperty.RegisterAttached( | |
"IsEnabled", | |
typeof(bool), | |
typeof(Slider), | |
new FrameworkPropertyMetadata( | |
false, | |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, | |
(o, e) => OnIsEnabledChanged((Slider) o, (bool) e.OldValue, (bool) e.NewValue))); | |
public static readonly DependencyProperty AnimateProperty = | |
DependencyProperty.RegisterAttached( | |
"Animate", | |
typeof(bool), | |
typeof(Slider), | |
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); | |
public static readonly DependencyProperty AnimationDurationProperty = | |
DependencyProperty.RegisterAttached( | |
"AnimationDuration", | |
typeof(Duration), | |
typeof(Slider), | |
new FrameworkPropertyMetadata(new Duration(TimeSpan.FromMilliseconds(200.0d)), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); | |
public static readonly DependencyProperty AnimationAccelerationRatioProperty = | |
DependencyProperty.RegisterAttached( | |
"AnimationAccelerationRatio", | |
typeof(double), | |
typeof(Slider), | |
new FrameworkPropertyMetadata(0.2d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); | |
public static readonly DependencyProperty AnimationDecelerationRatioProperty = | |
DependencyProperty.RegisterAttached( | |
"AnimationDecelerationRatio", | |
typeof(double), | |
typeof(Slider), | |
new FrameworkPropertyMetadata(0.8d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); | |
public static readonly DependencyProperty DefaultValueProperty = | |
DependencyProperty.RegisterAttached( | |
"DefaultValue", | |
typeof(double), | |
typeof(Slider), | |
new FrameworkPropertyMetadata( | |
0.0d, | |
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, | |
null, | |
(o, v) => VerifyDefaultValueRange((Slider) o, (double) v))); | |
private static double VerifyDefaultValueRange(Slider slider, double value) { | |
double minimum = slider.Minimum; | |
double num = (double) value; | |
if (num < minimum) { | |
return minimum; | |
} | |
double maximum = slider.Maximum; | |
if (num > maximum) { | |
return maximum; | |
} | |
return value; | |
} | |
public static bool GetIsEnabled(Slider o) { | |
return (bool) o.GetValue(IsEnabledProperty); | |
} | |
public static void SetIsEnabled(Slider o, bool value) { | |
o.SetValue(IsEnabledProperty, value); | |
} | |
public static bool GetAnimate(Slider o) { | |
return (bool) o.GetValue(AnimateProperty); | |
} | |
public static void SetAnimate(Slider o, bool value) { | |
o.SetValue(AnimateProperty, value); | |
} | |
public static double GetDefaultValue(Slider o) { | |
return (double) o.GetValue(DefaultValueProperty); | |
} | |
public static void SetDefaultValue(Slider o, double value) { | |
o.SetValue(DefaultValueProperty, value); | |
} | |
public static double GetAnimationAccelerationRatio(Slider o) { | |
return (double) o.GetValue(AnimationAccelerationRatioProperty); | |
} | |
public static void SetAnimationAccelerationRatio(Slider o, double value) { | |
o.SetValue(AnimationAccelerationRatioProperty, value); | |
} | |
public static double GetAnimationDecelerationRatio(Slider o) { | |
return (double) o.GetValue(AnimationDecelerationRatioProperty); | |
} | |
public static void SetAnimationDecelerationRatio(Slider o, double value) { | |
o.SetValue(AnimationDecelerationRatioProperty, value); | |
} | |
public static Duration GetAnimationDuration(Slider o) { | |
return (Duration) o.GetValue(AnimationDurationProperty); | |
} | |
public static void SetAnimationDuration(Slider o, Duration value) { | |
o.SetValue(AnimationDurationProperty, value); | |
} | |
private static void OnIsEnabledChanged(Slider slider, bool oldValue, bool newValue) { | |
if (oldValue == newValue) { | |
return; | |
} | |
if (newValue) { | |
slider.MouseDown += Slider_MouseDown; | |
} | |
else { | |
slider.MouseDown -= Slider_MouseDown; | |
} | |
} | |
private static void Slider_MouseDown(object sender, MouseButtonEventArgs e) { | |
if (e.ChangedButton == MouseButton.Middle) { | |
if (e.ButtonState == MouseButtonState.Pressed) { | |
SetSliderValueToDefault((Slider) sender, true); | |
} | |
} | |
} | |
private static void SetSliderValueToDefault(Slider slider, bool animate) { | |
double value = GetDefaultValue(slider); | |
if (animate) { | |
DoubleAnimation animation = new DoubleAnimation(slider.Value, value, GetAnimationDuration(slider)); | |
animation.AccelerationRatio = GetAnimationAccelerationRatio(slider); | |
animation.DecelerationRatio = GetAnimationDecelerationRatio(slider); | |
slider.BeginAnimation(Slider.ValueProperty, animation); | |
} | |
else { | |
slider.Value = value; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment