4 using System
.Windows
.Controls
;
6 namespace MyUserControl
8 public partial class NumericUpDown
: System
.Windows
.Controls
.UserControl
11 /// Initializes a new instance of the NumericUpDownControl.
13 public NumericUpDown()
15 InitializeComponent();
20 /// Identifies the Value dependency property.
22 public static readonly DependencyProperty ValueProperty
=
23 DependencyProperty
.Register(
24 "Value", typeof(decimal), typeof(NumericUpDown
),
25 new FrameworkPropertyMetadata(MinValue
, new PropertyChangedCallback(OnValueChanged
),
26 new CoerceValueCallback(CoerceValue
)));
29 /// Gets or sets the value assigned to the control.
33 get { return (decimal)GetValue(ValueProperty); }
34 set { SetValue(ValueProperty, value); }
37 private static object CoerceValue(DependencyObject element
, object value)
39 decimal newValue
= (decimal)value;
40 NumericUpDown control
= (NumericUpDown
)element
;
42 newValue
= Math
.Max(MinValue
, Math
.Min(MaxValue
, newValue
));
47 private static void OnValueChanged(DependencyObject obj
, DependencyPropertyChangedEventArgs args
)
49 NumericUpDown control
= (NumericUpDown
)obj
;
51 RoutedPropertyChangedEventArgs
<decimal> e
= new RoutedPropertyChangedEventArgs
<decimal>(
52 (decimal)args
.OldValue
, (decimal)args
.NewValue
, ValueChangedEvent
);
53 control
.OnValueChanged(e
);
57 /// Identifies the ValueChanged routed event.
59 public static readonly RoutedEvent ValueChangedEvent
= EventManager
.RegisterRoutedEvent(
60 "ValueChanged", RoutingStrategy
.Bubble
,
61 typeof(RoutedPropertyChangedEventHandler
<decimal>), typeof(NumericUpDown
));
64 /// Occurs when the Value property changes.
66 public event RoutedPropertyChangedEventHandler
<decimal> ValueChanged
68 add { AddHandler(ValueChangedEvent, value); }
69 remove { RemoveHandler(ValueChangedEvent, value); }
73 /// Raises the ValueChanged event.
75 /// <param name="args">Arguments associated with the ValueChanged event.</param>
76 protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs
<decimal> args
)
81 private void upButton_Click(object sender
, EventArgs e
)
86 private void downButton_Click(object sender
, EventArgs e
)
91 private const decimal MinValue
= 0, MaxValue
= 100;
94 //</SnippetCodeBehind>