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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
/*************************************************************************************
Extended WPF Toolkit
Copyright (C) 2007-2013 Xceed Software Inc.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
For more features, controls, and fast professional support,
pick up the Plus Edition at http://xceed.com/wpf_toolkit
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Windows;
using System.Globalization;
using System.IO;
namespace Tango.BrushPicker
{
class DoubleUpDown : UpDownBase
{
protected delegate double FromText(string s, NumberStyles style, IFormatProvider provider);
protected delegate double FromDecimal(decimal d);
private FromText _fromText;
private FromDecimal _fromDecimal;
private Func<double, double, bool> _fromLowerThan;
private Func<double, double, bool> _fromGreaterThan;
protected DoubleUpDown(FromText fromText, FromDecimal fromDecimal, Func<double, double, bool> fromLowerThan, Func<double, double, bool> fromGreaterThan)
{
if (fromText == null)
throw new ArgumentNullException("parseMethod");
if (fromDecimal == null)
throw new ArgumentNullException("fromDecimal");
if (fromLowerThan == null)
throw new ArgumentNullException("fromLowerThan");
if (fromGreaterThan == null)
throw new ArgumentNullException("fromGreaterThan");
_fromText = fromText;
_fromDecimal = fromDecimal;
_fromLowerThan = fromLowerThan;
_fromGreaterThan = fromGreaterThan;
}
protected static void UpdateMetadata(Type type, double? increment, double? minValue, double? maxValue)
{
DefaultStyleKeyProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(type));
IncrementProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(increment));
MaximumProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(maxValue));
MinimumProperty.OverrideMetadata(type, new FrameworkPropertyMetadata(minValue));
}
private bool IsLowerThan(double? value1, double? value2)
{
if (value1 == null || value2 == null)
return false;
return _fromLowerThan(value1.Value, value2.Value);
}
private bool IsGreaterThan(double? value1, double? value2)
{
if (value1 == null || value2 == null)
return false;
return _fromGreaterThan(value1.Value, value2.Value);
}
private bool HandleNullSpin()
{
if (!Value.HasValue)
{
double forcedValue = (DefaultValue.HasValue)
? DefaultValue.Value
: default(double);
Value = CoerceValueMinMax(forcedValue);
return true;
}
else if (!Increment.HasValue)
{
return true;
}
return false;
}
private double CoerceValueMinMax(double value)
{
if (IsLowerThan(value, Minimum))
return Minimum;
else if (IsGreaterThan(value, Maximum))
return Maximum;
else
return value;
}
private void ValidateDefaultMinMax(double? value)
{
if (object.Equals(value, DefaultValue))
return;
if (IsLowerThan(value, Minimum))
throw new ArgumentOutOfRangeException("Minimum", String.Format("Value must be greater than MinValue of {0}", Minimum));
else if (IsGreaterThan(value, Maximum))
throw new ArgumentOutOfRangeException("Maximum", String.Format("Value must be less than MaxValue of {0}", Maximum));
}
#region Base Class Overrides
protected override void OnIncrement()
{
if (!HandleNullSpin())
{
double? result = Value.Value + Increment.Value;
Value = CoerceValueMinMax(result.Value);
}
}
protected override void OnDecrement()
{
if (!HandleNullSpin())
{
double? result = Value.Value - Increment.Value;
Value = CoerceValueMinMax(result.Value);
}
}
protected override string ConvertValueToText()
{
if (Value == null)
return string.Empty;
return Value.Value.ToString(FormatString, CultureInfo);
}
protected override double? ConvertTextToValue(string text)
{
double? result = 0;
if (String.IsNullOrEmpty(text))
return result;
string currentValueText = ConvertValueToText();
if (object.Equals(currentValueText, text))
return this.Value;
result = FormatString.Contains("P")
? _fromDecimal(ParsePercent(text, CultureInfo))
: _fromText(text, this.ParsingNumberStyle, CultureInfo);
ValidateDefaultMinMax(result);
return result;
}
protected override void SetValidSpinDirection()
{
ValidSpinDirections validDirections = ValidSpinDirections.None;
if ((this.Increment != null) && !IsReadOnly)
{
if (IsLowerThan(Value, Maximum) || !Value.HasValue)
validDirections = validDirections | ValidSpinDirections.Increase;
if (IsGreaterThan(Value, Minimum) || !Value.HasValue)
validDirections = validDirections | ValidSpinDirections.Decrease;
}
if (Spinner != null)
Spinner.ValidSpinDirection = validDirections;
}
#endregion
#region Constructors
static DoubleUpDown()
{
UpdateMetadata(typeof(DoubleUpDown), 1d, double.NegativeInfinity, double.PositiveInfinity);
}
public DoubleUpDown()
: this(Double.Parse, Decimal.ToDouble, (v1, v2) => v1 < v2, (v1, v2) => v1 > v2)
{
}
#endregion
}
}
|