Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Spinner.cs « Xamarin.PropertyEditing.Windows - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19276213fea1d69ffc5efcf47d84f81b6d57a652 (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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Xamarin.PropertyEditing.Windows
{
	[TemplatePart (Name = "up", Type = typeof(ButtonBase))]
	[TemplatePart (Name = "down", Type = typeof(ButtonBase))]
	internal class Spinner
		: Control
	{
		static Spinner ()
		{
			DefaultStyleKeyProperty.OverrideMetadata (typeof(Spinner), new FrameworkPropertyMetadata (typeof(Spinner)));
			FocusableProperty.OverrideMetadata (typeof(Spinner), new FrameworkPropertyMetadata (false));
		}

		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register (
			"Value", typeof(int), typeof(Spinner), new FrameworkPropertyMetadata (default(int), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d,p) => ((Spinner)d).OnValueChanged ()));

		public int Value
		{
			get { return (int) GetValue (ValueProperty); }
			set { SetValue (ValueProperty, value); }
		}

		public static readonly DependencyProperty MinimumValueProperty = DependencyProperty.Register (
			"MinimumValue", typeof(int), typeof(Spinner), new PropertyMetadata (default(int)));

		public int MinimumValue
		{
			get { return (int) GetValue (MinimumValueProperty); }
			set { SetValue (MinimumValueProperty, value); }
		}

		public override void OnApplyTemplate ()
		{
			base.OnApplyTemplate ();

			this.up = GetTemplateChild ("up") as ButtonBase;
			this.down = GetTemplateChild ("down") as ButtonBase;
			this.display = GetTemplateChild("display") as TextBlock;

			if (this.up == null || this.down == null)
				throw new InvalidOperationException ("Spinner's template needs an up and down button");

			this.up.Click += (sender, args) => {
				args.Handled = true;
				Adjust (1);
			};
			this.down.Click += (sender, args) => {
				args.Handled = true;
				Adjust (-1);
			};

			Adjust (MinimumValue);
			OnValueChanged();
		}

		private ButtonBase up, down;
		private TextBlock display;

		private void Adjust (int d)
		{
			SetCurrentValue (ValueProperty, Value + d);
			this.down.IsEnabled = Value > MinimumValue;
		}

		private void OnValueChanged ()
		{
			if (this.display == null)
				return;

			this.display.Text = Value.ToString ();
		}
	}
}