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
|
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Xamarin.PropertyEditing.Windows
{
internal class DoubleUpDownControl
: NumericUpDownControl<double>
{
// TODO decimal placement
static DoubleUpDownControl ()
{
DefaultStyleKeyProperty.OverrideMetadata (typeof(DoubleUpDownControl), new FrameworkPropertyMetadata (typeof(DoubleUpDownControl)));
}
protected override bool TryParse (string text, out double value)
{
if (text == nameof(Double.NaN)) {
value = Double.NaN;
return true;
} else if (text == "∞" || text == nameof (Double.PositiveInfinity)) {
value = Double.PositiveInfinity;
return true;
} else if (text == "-∞" || text == nameof (Double.NegativeInfinity)) {
value = Double.NegativeInfinity;
return true;
}
return Double.TryParse (text, out value);
}
}
internal class IntegerUpDownControl
: NumericUpDownControl<long>
{
static IntegerUpDownControl ()
{
DefaultStyleKeyProperty.OverrideMetadata (typeof(IntegerUpDownControl), new FrameworkPropertyMetadata (typeof(IntegerUpDownControl)));
}
protected override bool TryParse (string text, out long value)
{
return Int64.TryParse (text, out value);
}
}
internal class ByteUpDownControl
: NumericUpDownControl<byte>
{
static ByteUpDownControl ()
{
DefaultStyleKeyProperty.OverrideMetadata (typeof (ByteUpDownControl), new FrameworkPropertyMetadata (typeof (ByteUpDownControl)));
}
protected override bool TryParse (string text, out byte value)
{
return byte.TryParse (text, out value);
}
}
internal abstract class NumericUpDownControl<T>
: TextBoxEx
where T : struct, IComparable<T>
{
public NumericUpDownControl ()
{
SetCurrentValue (TextProperty, Value.ToString());
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register (
"Value", typeof(T?), typeof(NumericUpDownControl<T>), new FrameworkPropertyMetadata (default(T?), (d,e) => ((NumericUpDownControl<T>)d).OnValueChanged (e)) { BindsTwoWayByDefault = true });
public T? Value
{
get { return (T?) GetValue (ValueProperty); }
set { SetValue (ValueProperty, value); }
}
protected abstract bool TryParse (string text, out T value);
protected override void OnPreviewKeyDown (KeyEventArgs e)
{
if (e.Key == Key.Down) {
SetCurrentValue (ValueProperty, Numeric<T?>.Decrement (Value));
SelectAll();
e.Handled = true;
} else if (e.Key == Key.Up) {
SetCurrentValue (ValueProperty, Numeric<T?>.Increment (Value));
SelectAll();
e.Handled = true;
}
base.OnPreviewKeyDown (e);
}
protected override void OnSubmit ()
{
base.OnSubmit ();
T value;
if (TryParse (Text, out value)) {
SetCurrentValue (ValueProperty, value);
} else {
SetCurrentValue (TextProperty, Value.ToString());
}
}
private void OnValueChanged (DependencyPropertyChangedEventArgs e)
{
SetCurrentValue (TextProperty, Value.ToString());
}
}
}
|