diff options
Diffstat (limited to 'Xamarin.Forms.Core/Slider.cs')
-rw-r--r-- | Xamarin.Forms.Core/Slider.cs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Slider.cs b/Xamarin.Forms.Core/Slider.cs new file mode 100644 index 00000000..6363c410 --- /dev/null +++ b/Xamarin.Forms.Core/Slider.cs @@ -0,0 +1,85 @@ +using System; +using Xamarin.Forms.Platform; + +namespace Xamarin.Forms +{ + [RenderWith(typeof(_SliderRenderer))] + public class Slider : View + { + public static readonly BindableProperty MinimumProperty = BindableProperty.Create("Minimum", typeof(double), typeof(Slider), 0d, validateValue: (bindable, value) => + { + var slider = (Slider)bindable; + return (double)value < slider.Maximum; + }, coerceValue: (bindable, value) => + { + var slider = (Slider)bindable; + slider.Value = slider.Value.Clamp((double)value, slider.Maximum); + return value; + }); + + public static readonly BindableProperty MaximumProperty = BindableProperty.Create("Maximum", typeof(double), typeof(Slider), 1d, validateValue: (bindable, value) => + { + var slider = (Slider)bindable; + return (double)value > slider.Minimum; + }, coerceValue: (bindable, value) => + { + var slider = (Slider)bindable; + slider.Value = slider.Value.Clamp(slider.Minimum, (double)value); + return value; + }); + + public static readonly BindableProperty ValueProperty = BindableProperty.Create("Value", typeof(double), typeof(Slider), 0d, BindingMode.TwoWay, coerceValue: (bindable, value) => + { + var slider = (Slider)bindable; + return ((double)value).Clamp(slider.Minimum, slider.Maximum); + }, propertyChanged: (bindable, oldValue, newValue) => + { + var slider = (Slider)bindable; + EventHandler<ValueChangedEventArgs> eh = slider.ValueChanged; + if (eh != null) + eh(slider, new ValueChangedEventArgs((double)oldValue, (double)newValue)); + }); + + public Slider() + { + } + + public Slider(double min, double max, double val) + { + if (min >= max) + throw new ArgumentOutOfRangeException("min"); + + if (max > Minimum) + { + Maximum = max; + Minimum = min; + } + else + { + Minimum = min; + Maximum = max; + } + Value = val.Clamp(min, max); + } + + public double Maximum + { + get { return (double)GetValue(MaximumProperty); } + set { SetValue(MaximumProperty, value); } + } + + public double Minimum + { + get { return (double)GetValue(MinimumProperty); } + set { SetValue(MinimumProperty, value); } + } + + public double Value + { + get { return (double)GetValue(ValueProperty); } + set { SetValue(ValueProperty, value); } + } + + public event EventHandler<ValueChangedEventArgs> ValueChanged; + } +}
\ No newline at end of file |