3 using System
.Windows
.Controls
;
4 using System
.Windows
.Input
;
6 namespace MyCustomControl
8 public partial class NumericUpDown
: Control
11 /// Initializes a new instance of the NumericUpDownControl.
13 public NumericUpDown()
18 static NumericUpDown()
20 DefaultStyleKeyProperty
.OverrideMetadata(typeof(NumericUpDown
),
21 new FrameworkPropertyMetadata(typeof(NumericUpDown
)));
25 /// Gets or sets the value assigned to the control.
29 get { return (decimal)GetValue(ValueProperty); }
30 set { SetValue(ValueProperty, value); }
34 /// Identifies the Value dependency property.
36 public static readonly DependencyProperty ValueProperty
=
37 DependencyProperty
.Register(
38 "Value", typeof(decimal), typeof(NumericUpDown
),
39 new FrameworkPropertyMetadata(MinValue
, new PropertyChangedCallback(OnValueChanged
),
40 new CoerceValueCallback(CoerceValue
)));
42 private static object CoerceValue(DependencyObject element
, object value)
44 decimal newValue
= (decimal)value;
46 newValue
= Math
.Max(MinValue
, Math
.Min(MaxValue
, newValue
));
51 private static void OnValueChanged(DependencyObject obj
, DependencyPropertyChangedEventArgs args
)
53 NumericUpDown control
= (NumericUpDown
)obj
;
55 RoutedPropertyChangedEventArgs
<decimal> e
= new RoutedPropertyChangedEventArgs
<decimal>(
56 (decimal)args
.OldValue
, (decimal)args
.NewValue
, ValueChangedEvent
);
57 control
.OnValueChanged(e
);
60 /// Identifies the ValueChanged routed event.
62 public static readonly RoutedEvent ValueChangedEvent
= EventManager
.RegisterRoutedEvent(
63 "ValueChanged", RoutingStrategy
.Bubble
,
64 typeof(RoutedPropertyChangedEventHandler
<decimal>), typeof(NumericUpDown
));
67 /// Occurs when the Value property changes.
69 public event RoutedPropertyChangedEventHandler
<decimal> ValueChanged
71 add { AddHandler(ValueChangedEvent, value); }
72 remove { RemoveHandler(ValueChangedEvent, value); }
75 /// Raises the ValueChanged event.
77 /// <param name="args">Arguments associated with the ValueChanged event.</param>
78 protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs
<decimal> args
)
85 public static RoutedCommand IncreaseCommand
89 return _increaseCommand
;
92 public static RoutedCommand DecreaseCommand
96 return _decreaseCommand
;
100 private static void InitializeCommands()
102 _increaseCommand
= new RoutedCommand("IncreaseCommand", typeof(NumericUpDown
));
103 CommandManager
.RegisterClassCommandBinding(typeof(NumericUpDown
),
104 new CommandBinding(_increaseCommand
, OnIncreaseCommand
));
105 CommandManager
.RegisterClassInputBinding(typeof(NumericUpDown
),
106 new InputBinding(_increaseCommand
, new KeyGesture(Key
.Up
)));
108 _decreaseCommand
= new RoutedCommand("DecreaseCommand", typeof(NumericUpDown
));
109 CommandManager
.RegisterClassCommandBinding(typeof(NumericUpDown
),
110 new CommandBinding(_decreaseCommand
, OnDecreaseCommand
));
111 CommandManager
.RegisterClassInputBinding(typeof(NumericUpDown
),
112 new InputBinding(_decreaseCommand
, new KeyGesture(Key
.Down
)));
115 private static void OnIncreaseCommand(object sender
, ExecutedRoutedEventArgs e
)
117 NumericUpDown control
= sender
as NumericUpDown
;
120 control
.OnIncrease();
123 private static void OnDecreaseCommand(object sender
, ExecutedRoutedEventArgs e
)
125 NumericUpDown control
= sender
as NumericUpDown
;
128 control
.OnDecrease();
132 protected virtual void OnIncrease()
136 protected virtual void OnDecrease()
141 private static RoutedCommand _increaseCommand
;
142 private static RoutedCommand _decreaseCommand
;
146 private const decimal MinValue
= 0, MaxValue
= 100;