added samples
[windows-sources.git] / sdk / samples / WPFSamples / CustomControlNumericUpDownOneProject / csharp / numericupdown.cs
blobccd84303e1e949577ca9681ff0b534379b725816
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Input;
6 namespace MyCustomControl
8 public partial class NumericUpDown : Control
10 /// <summary>
11 /// Initializes a new instance of the NumericUpDownControl.
12 /// </summary>
13 public NumericUpDown()
15 InitializeCommands();
18 static NumericUpDown()
20 DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown),
21 new FrameworkPropertyMetadata(typeof(NumericUpDown)));
24 /// <summary>
25 /// Gets or sets the value assigned to the control.
26 /// </summary>
27 public decimal Value
29 get { return (decimal)GetValue(ValueProperty); }
30 set { SetValue(ValueProperty, value); }
33 /// <summary>
34 /// Identifies the Value dependency property.
35 /// </summary>
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));
48 return 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);
59 /// <summary>
60 /// Identifies the ValueChanged routed event.
61 /// </summary>
62 public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
63 "ValueChanged", RoutingStrategy.Bubble,
64 typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));
66 /// <summary>
67 /// Occurs when the Value property changes.
68 /// </summary>
69 public event RoutedPropertyChangedEventHandler<decimal> ValueChanged
71 add { AddHandler(ValueChangedEvent, value); }
72 remove { RemoveHandler(ValueChangedEvent, value); }
74 /// <summary>
75 /// Raises the ValueChanged event.
76 /// </summary>
77 /// <param name="args">Arguments associated with the ValueChanged event.</param>
78 protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args)
80 RaiseEvent(args);
83 #region Commands
85 public static RoutedCommand IncreaseCommand
87 get
89 return _increaseCommand;
92 public static RoutedCommand DecreaseCommand
94 get
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;
118 if (control != null)
120 control.OnIncrease();
123 private static void OnDecreaseCommand(object sender, ExecutedRoutedEventArgs e)
125 NumericUpDown control = sender as NumericUpDown;
126 if (control != null)
128 control.OnDecrease();
132 protected virtual void OnIncrease()
134 Value++;
136 protected virtual void OnDecrease()
138 Value--;
141 private static RoutedCommand _increaseCommand;
142 private static RoutedCommand _decreaseCommand;
144 #endregion
146 private const decimal MinValue = 0, MaxValue = 100;