Repository navigation
Expand file tree
/
Copy pathIntegerBox.cs
More file actions
351 lines (308 loc) · 11.9 KB
/
Copy pathIntegerBox.cs
File metadata and controls
351 lines (308 loc) · 11.9 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
/// <summary>
/// A simple IntegerBox for WPF that supports Maximum, Minimum and (Left)Pad.
/// </summary>
public class IntegerBox : UserControl
{
/// <summary>
/// The dependency property for the integer value entered in the TextBox.
/// </summary>
public static readonly DependencyProperty IntegerValueProperty = DependencyProperty.Register(
nameof(IntegerValue),
typeof(int?),
typeof(IntegerBox),
new FrameworkPropertyMetadata(default(int?),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
IntegerValuePropertyChangedCallback,
CoerceIntegerValueCallback));
/// <summary>
/// The dependency property for the left pad.
/// </summary>
public static readonly DependencyProperty PadProperty = DependencyProperty.Register(
nameof(Pad),
typeof(int),
typeof(IntegerBox),
new FrameworkPropertyMetadata(default(int),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
PadPropertyChangedCallback));
/// <summary>
/// The dependency property for the minimum value.
/// </summary>
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register(
nameof(Minimum),
typeof(int?),
typeof(IntegerBox),
new FrameworkPropertyMetadata(default(int?),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
MinimumPropertyChangedCallback,
CoerceMinimumPropertyCallback));
/// <summary>
/// The dependency property for the maximum value.
/// </summary>
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register(
nameof(Maximum),
typeof(int?),
typeof(IntegerBox),
new FrameworkPropertyMetadata(default(int?),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
MaximumPropertyChangedCallback,
CoerceMaximumPropertyCallback));
/// <summary>
/// The integer value entered through the IntegerBox.
/// </summary>
/// <remarks>
/// The value can be <value>null</value> if <see cref="Pad"/> is <value>0</value>.
/// </remarks>
public int? IntegerValue
{
get => (int?) GetValue(IntegerValueProperty);
set => SetValue(IntegerValueProperty, value);
}
/// <summary>
/// The left pad for the TextBox. Determines the minimum number of digits displayed in the TextBox.
/// </summary>
/// <remarks>
/// The Pad property is ignored while the IntegerBox or it's children have keyboard focus.
/// This makes the TextBox easier to use.
/// </remarks>
public int Pad
{
get => (int) GetValue(PadProperty);
set => SetValue(PadProperty, value);
}
/// <summary>
/// The minimum value that can be entered. <value>null</value> equals infinite.
/// </summary>
public int? Minimum
{
get => (int?) GetValue(MinimumProperty);
set => SetValue(MinimumProperty, value);
}
/// <summary>
/// The maximum value that can be entered. <value>null</value> equals infinite.
/// </summary>
public int? Maximum
{
get => (int?) GetValue(MaximumProperty);
set => SetValue(MaximumProperty, value);
}
public IntegerBox()
{
OnApplyTemplate();
}
/// <summary>
/// Validate that a string is a integer number.
/// </summary>
/// <param name="text">The string to test.</param>
/// <returns>
/// <value>true</value> if the string is a integer number.
/// </returns>
/// <remarks>
/// The method does not range check. The number can be out of bounds.
/// </remarks>
private static bool ValidateText(string text)
{
return new Regex("^-?[0-9]*$").IsMatch(text);
}
public override void OnApplyTemplate()
{
// Attempt to find the TextBox internally used for input
if (Template?.FindName("PART_TextBox", this) is TextBox partTextBox)
{
partTextBox.PreviewTextInput += OnPreviewTextInput;
// partTextBox.PreviewKeyDown += OnPreviewKeyDown;
partTextBox.LostFocus += OnLostFocus;
partTextBox.TextChanged += OnTextChanged;
DataObject.AddPastingHandler(partTextBox, OnPaste);
RefreshText();
}
base.OnApplyTemplate();
}
/// <summary>
/// Handle <see cref="IntegerValue"/> value changed. Refreshes Text.
/// </summary>
private static void IntegerValuePropertyChangedCallback(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var integerBox = (IntegerBox) d;
integerBox.RefreshText();
}
/// <summary>
/// Handle <see cref="Minimum"/> value changed. Coerces <see cref="IntegerValue"/>.
/// </summary>
private static void MinimumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var integerBox = (IntegerBox) d;
integerBox.CoerceValue(IntegerValueProperty);
}
/// <summary>
/// Handle <see cref="Maximum"/> value changed. Coerces <see cref="IntegerValue"/>.
/// </summary>
private static void MaximumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var integerBox = (IntegerBox) d;
integerBox.CoerceValue(IntegerValueProperty);
}
/// <summary>
/// Handle <see cref="Pad"/> value changed. Refreshes Text of the TextBox.
/// </summary>
private static void PadPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var integerBox = (IntegerBox) d;
integerBox.RefreshText();
}
/// <summary>
/// Coerces <see cref="IntegerValue"/>. Ensures that the integer value stays within bounds.
/// </summary>
/// <param name="d">The dependency object.</param>
/// <param name="baseValue">The input value.</param>
/// <returns>Returns the coerced value.</returns>
private static object CoerceIntegerValueCallback(DependencyObject d, object baseValue)
{
var integerBox = (IntegerBox) d;
var value = (int?) baseValue;
if (value == null) return null;
if (integerBox.Minimum != null && integerBox.Minimum > value) value = integerBox.Minimum;
if (integerBox.Maximum != null && integerBox.Maximum < value) value = integerBox.Maximum;
return value;
}
/// <summary>
/// Coerces <see cref="Minimum"/>. Ensures that the minimum value stays below the maximum.
/// </summary>
/// <param name="d">The dependency object.</param>
/// <param name="baseValue">The input value.</param>
/// <returns>Returns the coerced value.</returns>
private static object CoerceMinimumPropertyCallback(DependencyObject d, object baseValue)
{
var integerBox = (IntegerBox) d;
var value = (int?) baseValue;
if (value == null) return null;
if (integerBox.Maximum != null && integerBox.Maximum < value) value = integerBox.Maximum;
return value;
}
/// <summary>
/// Coerces <see cref="Maximum"/>. Ensures that the minimum value stays above the minimum.
/// </summary>
/// <param name="d">The dependency object.</param>
/// <param name="baseValue">The input value.</param>
/// <returns>Returns the coerced value.</returns>
private static object CoerceMaximumPropertyCallback(DependencyObject d, object baseValue)
{
var integerBox = (IntegerBox) d;
var value = (int?) baseValue;
if (value == null) return null;
if (integerBox.Minimum != null && integerBox.Minimum > value) value = integerBox.Minimum;
return value;
}
/// <summary>
/// Handle <see cref="TextBox.TextChanged"/> event of the TextBox used internally for the input.
/// </summary>
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (!(sender is TextBox textBox)) return;
UpdateValue(textBox.Text);
}
/// <summary>
/// Handle OnPaste event of the TextBox used internally for the input.
/// Cancels the operation if the inserted text would result in the updated text not being a valid number.
/// </summary>
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
if (!(sender is TextBox textBox)) return;
if (!e.SourceDataObject.GetDataPresent(DataFormats.Text))
{
return;
}
var clipboard = Convert.ToString(e.DataObject.GetData(DataFormats.Text));
var text = textBox.SelectionLength > 0
? textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength)
: textBox.Text;
text = text.Insert(textBox.CaretIndex, clipboard);
if (!ValidateText(text))
{
e.CancelCommand();
}
}
/// <summary>
/// Handle PreviewTextInput event of the TextBox used internally for the input.
/// Cancels the operation if the inserted text would result in the updated text not being a valid number.
/// </summary>
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!(sender is TextBox textBox)) return;
var text = textBox.SelectionLength > 0
? textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength)
: textBox.Text;
text = text.Insert(textBox.CaretIndex, e.Text);
e.Handled = !ValidateText(text);
}
/// <summary>
/// Handle LostFocus event of the TextBox used internally for the input.
/// Refreshes the text.
/// </summary>
private void OnLostFocus(object sender, RoutedEventArgs e)
{
if (!(sender is TextBox _)) return;
RefreshText();
}
/// <summary>
/// Refreshes the text of the TextBox used internally for the input.
/// </summary>
private void RefreshText()
{
if (Template?.FindName("PART_TextBox", this) is TextBox partTextBox)
{
// If the integer value is null set the text to empty
if (!IntegerValue.HasValue)
{
partTextBox.Text = "";
return;
}
var number = Math.Abs(IntegerValue.Value).ToString();
// If the IntegerBox or a child has keyboard focus only refresh text without pad
if (IsKeyboardFocusWithin)
{
var caretIndex = partTextBox.CaretIndex;
partTextBox.Text = number;
if (caretIndex > 0 && number.Length <= Pad)
{
partTextBox.CaretIndex = caretIndex;
}
}
// Set text with pad
else
{
partTextBox.Text = IntegerValue.Value < 0
? "-" + number.PadLeft(Pad - 1, '0')
: number.PadLeft(Pad, '0');
}
}
}
/// <summary>
/// Update <see cref="IntegerValue"/> by string.
/// </summary>
private void UpdateValue(string text)
{
// If parsable just use value
if (int.TryParse(text, out var integer))
{
IntegerValue = integer;
}
// If not parsable to int, check if input is a number and just overflows
else if (Regex.IsMatch(text, "^-?[0-9]+$"))
{
IntegerValue = text[0] == '-'
? int.MinValue
: int.MaxValue;
}
else
{
IntegerValue = null;
}
RefreshText();
}
}