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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
/*************************************************************************************
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.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Globalization;
using System.Windows.Media;
namespace Tango.BrushPicker
{
[TemplatePart(Name = PART_TextBox, Type = typeof(TextBox))]
[TemplatePart(Name = PART_Spinner, Type = typeof(Spinner))]
abstract class UpDownBase : Control, IValidateInput
{
#region Members
internal const string PART_TextBox = "PART_TextBox";
internal const string PART_Spinner = "PART_Spinner";
private bool _isSyncingTextAndValueProperties;
private bool _isTextChangedFromUI;
#endregion
#region Properties
internal Spinner Spinner
{
get;
private set;
}
internal TextBox TextBox
{
get;
private set;
}
#region CultureInfo
public static readonly DependencyProperty CultureInfoProperty = DependencyProperty.Register("CultureInfo", typeof(CultureInfo), typeof(UpDownBase), new UIPropertyMetadata(CultureInfo.CurrentCulture, OnCultureInfoChanged));
public CultureInfo CultureInfo
{
get
{
return (CultureInfo)GetValue(CultureInfoProperty);
}
set
{
SetValue(CultureInfoProperty, value);
}
}
private static void OnCultureInfoChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase inputBase = o as UpDownBase;
if (inputBase != null)
inputBase.OnCultureInfoChanged((CultureInfo)e.OldValue, (CultureInfo)e.NewValue);
}
#endregion //CultureInfo
#region IsReadOnly
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UpDownBase), new UIPropertyMetadata(false, OnReadOnlyChanged));
public bool IsReadOnly
{
get
{
return (bool)GetValue(IsReadOnlyProperty);
}
set
{
SetValue(IsReadOnlyProperty, value);
}
}
private static void OnReadOnlyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase inputBase = o as UpDownBase;
if (inputBase != null)
inputBase.OnReadOnlyChanged((bool)e.OldValue, (bool)e.NewValue);
}
#endregion //IsReadOnly
#region Text
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UpDownBase), new FrameworkPropertyMetadata(default(String), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged, null, false, UpdateSourceTrigger.LostFocus));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
private static void OnTextChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase inputBase = o as UpDownBase;
if (inputBase != null)
inputBase.OnTextChanged((string)e.OldValue, (string)e.NewValue);
}
#endregion //Text
#region FormatString
public static readonly DependencyProperty FormatStringProperty = DependencyProperty.Register("FormatString", typeof(string), typeof(UpDownBase), new UIPropertyMetadata(String.Empty, OnFormatStringChanged));
public string FormatString
{
get
{
return (string)GetValue(FormatStringProperty);
}
set
{
SetValue(FormatStringProperty, value);
}
}
private static void OnFormatStringChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase numericUpDown = o as UpDownBase;
if (numericUpDown != null)
numericUpDown.OnFormatStringChanged((string)e.OldValue, (string)e.NewValue);
}
protected virtual void OnFormatStringChanged(string oldValue, string newValue)
{
if (IsInitialized)
{
this.SyncTextAndValue(false, null);
}
}
#endregion //FormatString
#region Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double?), typeof(UpDownBase), new PropertyMetadata(default(double), OnIncrementChanged, OnCoerceIncrement));
public double? Increment
{
get
{
return (double?)GetValue(IncrementProperty);
}
set
{
SetValue(IncrementProperty, value);
}
}
private static void OnIncrementChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase numericUpDown = o as UpDownBase;
if (numericUpDown != null)
numericUpDown.OnIncrementChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnIncrementChanged(double oldValue, double newValue)
{
if (this.IsInitialized)
{
SetValidSpinDirection();
}
}
private static object OnCoerceIncrement(DependencyObject d, object baseValue)
{
UpDownBase numericUpDown = d as UpDownBase;
if (numericUpDown != null)
return numericUpDown.OnCoerceIncrement((double)baseValue);
return baseValue;
}
protected virtual double? OnCoerceIncrement(double? baseValue)
{
return baseValue;
}
#endregion
#region Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(UpDownBase), new UIPropertyMetadata(default(double), OnMaximumChanged, OnCoerceMaximum));
public double Maximum
{
get
{
return (double)GetValue(MaximumProperty);
}
set
{
SetValue(MaximumProperty, value);
}
}
private static void OnMaximumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase numericUpDown = o as UpDownBase;
if (numericUpDown != null)
numericUpDown.OnMaximumChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnMaximumChanged(double oldValue, double newValue)
{
if (this.IsInitialized)
{
SetValidSpinDirection();
}
}
private static object OnCoerceMaximum(DependencyObject d, object baseValue)
{
UpDownBase numericUpDown = d as UpDownBase;
if (numericUpDown != null)
return numericUpDown.OnCoerceMaximum((double)baseValue);
return baseValue;
}
protected virtual double OnCoerceMaximum(double baseValue)
{
return baseValue;
}
#endregion //Maximum
#region Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(UpDownBase), new UIPropertyMetadata(default(double), OnMinimumChanged, OnCoerceMinimum));
public double Minimum
{
get
{
return (double)GetValue(MinimumProperty);
}
set
{
SetValue(MinimumProperty, value);
}
}
private static void OnMinimumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase numericUpDown = o as UpDownBase;
if (numericUpDown != null)
numericUpDown.OnMinimumChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnMinimumChanged(double oldValue, double newValue)
{
if (this.IsInitialized)
{
SetValidSpinDirection();
}
}
private static object OnCoerceMinimum(DependencyObject d, object baseValue)
{
UpDownBase numericUpDown = d as UpDownBase;
if (numericUpDown != null)
return numericUpDown.OnCoerceMinimum((double)baseValue);
return baseValue;
}
protected virtual double? OnCoerceMinimum(double baseValue)
{
return baseValue;
}
#endregion //Minimum
#region AllowSpin
public static readonly DependencyProperty AllowSpinProperty = DependencyProperty.Register("AllowSpin", typeof(bool), typeof(UpDownBase), new UIPropertyMetadata(true));
public bool AllowSpin
{
get
{
return (bool)GetValue(AllowSpinProperty);
}
set
{
SetValue(AllowSpinProperty, value);
}
}
#endregion //AllowSpin
#region DefaultValue
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(double?), typeof(UpDownBase), new UIPropertyMetadata(default(double), OnDefaultValueChanged));
public double? DefaultValue
{
get
{
return (double?)GetValue(DefaultValueProperty);
}
set
{
SetValue(DefaultValueProperty, value);
}
}
private static void OnDefaultValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
{
((UpDownBase)source).OnDefaultValueChanged((double)args.OldValue, (double)args.NewValue);
}
private void OnDefaultValueChanged(double oldValue, double newValue)
{
if (this.IsInitialized && string.IsNullOrEmpty(Text))
{
this.SyncTextAndValue(true, Text);
}
}
#endregion //DefaultValue
#region AllowInputSpecialValues
static readonly DependencyProperty AllowInputSpecialValuesProperty =
DependencyProperty.Register("AllowInputSpecialValues", typeof(AllowedSpecialValues), typeof(UpDownBase), new UIPropertyMetadata(AllowedSpecialValues.None));
AllowedSpecialValues AllowInputSpecialValues
{
get { return (AllowedSpecialValues)GetValue(AllowInputSpecialValuesProperty); }
set { SetValue(AllowInputSpecialValuesProperty, value); }
}
#endregion //AllowInputSpecialValues
#region ParsingNumberStyle
public static readonly DependencyProperty ParsingNumberStyleProperty =
DependencyProperty.Register("ParsingNumberStyle", typeof(NumberStyles), typeof(UpDownBase), new UIPropertyMetadata(NumberStyles.Any));
public NumberStyles ParsingNumberStyle
{
get { return (NumberStyles)GetValue(ParsingNumberStyleProperty); }
set { SetValue(ParsingNumberStyleProperty, value); }
}
#endregion
#region Value
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double?), typeof(UpDownBase), new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged, OnCoerceValue, false, UpdateSourceTrigger.PropertyChanged));
public double? Value
{
get
{
return (double?)GetValue(ValueProperty);
}
set
{
SetValue(ValueProperty, value);
}
}
private static object OnCoerceValue(DependencyObject o, object basevalue)
{
return ((UpDownBase)o).OnCoerceValue(basevalue);
}
protected virtual object OnCoerceValue(object newValue)
{
return newValue;
}
private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
UpDownBase upDownBase = o as UpDownBase;
if (upDownBase != null)
upDownBase.OnValueChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnValueChanged(double oldValue, double newValue)
{
if (this.IsInitialized)
{
SyncTextAndValue(false, null);
}
SetValidSpinDirection();
RoutedPropertyChangedEventArgs<object> args = new RoutedPropertyChangedEventArgs<object>(oldValue, newValue);
args.RoutedEvent = ValueChangedEvent;
RaiseEvent(args);
}
#endregion //Value
#endregion //Properties
#region Base Class Overrides
protected override void OnAccessKey(AccessKeyEventArgs e)
{
if (TextBox != null)
TextBox.Focus();
base.OnAccessKey(e);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
TextBox = GetTemplateChild(PART_TextBox) as TextBox;
if (TextBox != null)
{
if (string.IsNullOrEmpty(Text))
TextBox.Text = "0.0";
else
TextBox.Text = Text;
TextBox.LostFocus += new RoutedEventHandler(TextBox_LostFocus);
TextBox.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
}
if (Spinner != null)
Spinner.Spin -= OnSpinnerSpin;
Spinner = GetTemplateChild(PART_Spinner) as Spinner;
if (Spinner != null)
Spinner.Spin += OnSpinnerSpin;
SetValidSpinDirection();
}
protected override void OnGotFocus(RoutedEventArgs e)
{
if (TextBox != null)
{
TextBox.Focus();
}
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
{
if (AllowSpin && !IsReadOnly)
DoIncrement();
e.Handled = true;
break;
}
case Key.Down:
{
if (AllowSpin && !IsReadOnly)
DoDecrement();
e.Handled = true;
break;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
{
bool commitSuccess = CommitInput();
e.Handled = !commitSuccess;
break;
}
}
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (!e.Handled && AllowSpin && !IsReadOnly && (TextBox.IsFocused))
{
if (e.Delta < 0)
{
DoDecrement();
}
else if (0 < e.Delta)
{
DoIncrement();
}
e.Handled = true;
}
}
protected void OnTextChanged(string oldValue, string newValue)
{
if (this.IsInitialized)
{
SyncTextAndValue(true, Text);
}
}
protected void OnCultureInfoChanged(CultureInfo oldValue, CultureInfo newValue)
{
if (IsInitialized)
{
SyncTextAndValue(false, null);
}
}
protected void OnReadOnlyChanged(bool oldValue, bool newValue)
{
SetValidSpinDirection();
}
protected void OnSpin(SpinEventArgs e)
{
if (e == null)
throw new ArgumentNullException("e");
if (e.Direction == SpinDirection.Increase)
DoIncrement();
else
DoDecrement();
}
#endregion
#region Event Handlers
private void OnSpinnerSpin(object sender, SpinEventArgs e)
{
if (AllowSpin && !IsReadOnly)
OnSpin(e);
}
#endregion
#region Events
public event InputValidationErrorEventHandler InputValidationError;
#region ValueChanged Event
public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<object>), typeof(UpDownBase));
public event RoutedPropertyChangedEventHandler<object> ValueChanged
{
add
{
AddHandler(ValueChangedEvent, value);
}
remove
{
RemoveHandler(ValueChangedEvent, value);
}
}
#endregion
#endregion //Events
#region Methods
private void DoDecrement()
{
if (Spinner == null)
{
OnDecrement();
}
}
private void DoIncrement()
{
if (Spinner == null)
{
OnIncrement();
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
_isTextChangedFromUI = true;
Text = ((TextBox)sender).Text;
}
finally
{
_isTextChangedFromUI = false;
}
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
CommitInput();
}
private void RaiseInputValidationError(Exception e)
{
if (InputValidationError != null)
{
InputValidationErrorEventArgs args = new InputValidationErrorEventArgs(e);
InputValidationError(this, args);
if (args.ThrowException)
{
throw args.Exception;
}
}
}
public bool CommitInput()
{
return this.SyncTextAndValue(true, Text);
}
protected bool SyncTextAndValue(bool updateValueFromText, string text)
{
if (_isSyncingTextAndValueProperties)
return true;
_isSyncingTextAndValueProperties = true;
bool parsedTextIsValid = true;
try
{
if (updateValueFromText)
{
if (string.IsNullOrEmpty(text))
{
// An empty input sets the value to the default value.
Value = this.DefaultValue;
}
else
{
try
{
Value = this.ConvertTextToValue(text);
}
catch (Exception e)
{
parsedTextIsValid = false;
// From the UI, just allow any input.
if (!_isTextChangedFromUI)
{
// This call may throw an exception.
// See RaiseInputValidationError() implementation.
this.RaiseInputValidationError(e);
}
}
}
}
// Do not touch the ongoing text input from user.
if (!_isTextChangedFromUI)
{
// Don't replace the empty Text with the non-empty representation of DefaultValue.
bool shouldKeepEmpty = string.IsNullOrEmpty(Text) && object.Equals(Value, DefaultValue);
if (!shouldKeepEmpty)
{
Text = ConvertValueToText();
}
// Sync Text and textBox
if (TextBox != null)
{
if (string.IsNullOrEmpty(Text))
TextBox.Text = "0.0";
else
TextBox.Text = Text;
}
}
if (_isTextChangedFromUI && !parsedTextIsValid)
{
//// Text input was made from the user and the text
//// repesents an invalid value. Disable the spinner
//// in this case.
if (Spinner != null)
{
Spinner.ValidSpinDirection = ValidSpinDirections.None;
}
}
else
{
this.SetValidSpinDirection();
}
}
finally
{
_isSyncingTextAndValueProperties = false;
}
return parsedTextIsValid;
}
protected static decimal ParsePercent(string text, IFormatProvider cultureInfo)
{
NumberFormatInfo info = NumberFormatInfo.GetInstance(cultureInfo);
text = text.Replace(info.PercentSymbol, null);
decimal result = Decimal.Parse(text, NumberStyles.Any, info);
result = result / 100;
return result;
}
#endregion
#region Abstract
protected abstract double? ConvertTextToValue(string text);
protected abstract string ConvertValueToText();
protected abstract void OnIncrement();
protected abstract void OnDecrement();
protected abstract void SetValidSpinDirection();
#endregion
}
}
|