summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs
blob: bf83bf1d66ad6a63388ce43c1964e89ba57205da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Xamarin.Forms.Platform.WinRT
{
	public sealed partial class StepperControl : UserControl
	{
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnValueChanged));

		public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnMaxMinChanged));

		public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnMaxMinChanged));

		public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(StepperControl),
			new PropertyMetadata(default(double), OnIncrementChanged));

		public static readonly DependencyProperty ButtonBackgroundColorProperty = DependencyProperty.Register(nameof(ButtonBackgroundColor), typeof(Color), typeof(StepperControl), new PropertyMetadata(default(Color), OnButtonBackgroundColorChanged));

		public StepperControl()
		{
			InitializeComponent();
		}

		public double Increment
		{
			get { return (double)GetValue(IncrementProperty); }
			set { SetValue(IncrementProperty, value); }
		}

		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 Color ButtonBackgroundColor
		{
			get { return (Color)GetValue(ButtonBackgroundColorProperty); }
			set { SetValue(ButtonBackgroundColorProperty, value); }
		}

		public event EventHandler ValueChanged;

		static void OnButtonBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateButtonBackgroundColor(stepper.ButtonBackgroundColor);
		}

		static void OnIncrementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled(stepper.Value);
		}

		static void OnMaxMinChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled(stepper.Value);
		}

		void OnMinusClicked(object sender, RoutedEventArgs e)
		{
			UpdateValue(-Increment);
		}

		void OnPlusClicked(object sender, RoutedEventArgs e)
		{
			UpdateValue(+Increment);
		}

		static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled((double)e.NewValue);

			EventHandler changed = stepper.ValueChanged;
			if (changed != null)
				changed(d, EventArgs.Empty);
		}

		void UpdateButtonBackgroundColor(Color value)
		{
			Windows.UI.Xaml.Media.Brush brush = value.ToBrush();
			Minus.Background = brush;
			Plus.Background = brush;
		}

		void UpdateEnabled(double value)
		{
			double increment = Increment;
			Plus.IsEnabled = value + increment <= Maximum;
			Minus.IsEnabled = value - increment >= Minimum;
		}

		void UpdateValue(double delta)
		{
			double newValue = Value + delta;
			Value = newValue;
		}
	}
}