Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| using System; | |
| using System.Windows; | |
| using System.Windows.Controls; | |
| using System.Windows.Input; | |
| namespace MyCustomControl | |
| { | |
| public partial class NumericUpDown : Control | |
| { | |
| /// <summary> | |
| /// Initializes a new instance of the NumericUpDownControl. | |
| /// </summary> | |
| public NumericUpDown() | |
| { | |
| InitializeCommands(); | |
| } | |
| //<SnippetStaticConstructor> | |
| static NumericUpDown() | |
| { | |
| DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), | |
| new FrameworkPropertyMetadata(typeof(NumericUpDown))); | |
| } | |
| //</SnippetStaticConstructor> | |
| /// <summary> | |
| /// Gets or sets the value assigned to the control. | |
| /// </summary> | |
| public decimal Value | |
| { | |
| get { return (decimal)GetValue(ValueProperty); } | |
| set { SetValue(ValueProperty, value); } | |
| } | |
| /// <summary> | |
| /// Identifies the Value dependency property. | |
| /// </summary> | |
| //<SnippetDependencyPropertyChangedEventArgs> | |
| public static readonly DependencyProperty ValueProperty = | |
| DependencyProperty.Register( | |
| "Value", typeof(decimal), typeof(NumericUpDown), | |
| new FrameworkPropertyMetadata(MinValue, new PropertyChangedCallback(OnValueChanged), | |
| new CoerceValueCallback(CoerceValue))); | |
| private static object CoerceValue(DependencyObject element, object value) | |
| { | |
| decimal newValue = (decimal)value; | |
| newValue = Math.Max(MinValue, Math.Min(MaxValue, newValue)); | |
| return newValue; | |
| } | |
| private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) | |
| { | |
| NumericUpDown control = (NumericUpDown)obj; | |
| RoutedPropertyChangedEventArgs<decimal> e = new RoutedPropertyChangedEventArgs<decimal>( | |
| (decimal)args.OldValue, (decimal)args.NewValue, ValueChangedEvent); | |
| control.OnValueChanged(e); | |
| } | |
| /// <summary> | |
| /// Identifies the ValueChanged routed event. | |
| /// </summary> | |
| public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent( | |
| "ValueChanged", RoutingStrategy.Bubble, | |
| typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown)); | |
| /// <summary> | |
| /// Occurs when the Value property changes. | |
| /// </summary> | |
| public event RoutedPropertyChangedEventHandler<decimal> ValueChanged | |
| { | |
| add { AddHandler(ValueChangedEvent, value); } | |
| remove { RemoveHandler(ValueChangedEvent, value); } | |
| } | |
| /// <summary> | |
| /// Raises the ValueChanged event. | |
| /// </summary> | |
| /// <param name="args">Arguments associated with the ValueChanged event.</param> | |
| protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args) | |
| { | |
| RaiseEvent(args); | |
| } | |
| //</SnippetDependencyPropertyChangedEventArgs> | |
| #region Commands | |
| //<SnippetCommands> | |
| public static RoutedCommand IncreaseCommand | |
| { | |
| get | |
| { | |
| return _increaseCommand; | |
| } | |
| } | |
| public static RoutedCommand DecreaseCommand | |
| { | |
| get | |
| { | |
| return _decreaseCommand; | |
| } | |
| } | |
| private static void InitializeCommands() | |
| { | |
| _increaseCommand = new RoutedCommand("IncreaseCommand", typeof(NumericUpDown)); | |
| CommandManager.RegisterClassCommandBinding(typeof(NumericUpDown), | |
| new CommandBinding(_increaseCommand, OnIncreaseCommand)); | |
| CommandManager.RegisterClassInputBinding(typeof(NumericUpDown), | |
| new InputBinding(_increaseCommand, new KeyGesture(Key.Up))); | |
| _decreaseCommand = new RoutedCommand("DecreaseCommand", typeof(NumericUpDown)); | |
| CommandManager.RegisterClassCommandBinding(typeof(NumericUpDown), | |
| new CommandBinding(_decreaseCommand, OnDecreaseCommand)); | |
| CommandManager.RegisterClassInputBinding(typeof(NumericUpDown), | |
| new InputBinding(_decreaseCommand, new KeyGesture(Key.Down))); | |
| } | |
| private static void OnIncreaseCommand(object sender, ExecutedRoutedEventArgs e) | |
| { | |
| NumericUpDown control = sender as NumericUpDown; | |
| if (control != null) | |
| { | |
| control.OnIncrease(); | |
| } | |
| } | |
| private static void OnDecreaseCommand(object sender, ExecutedRoutedEventArgs e) | |
| { | |
| NumericUpDown control = sender as NumericUpDown; | |
| if (control != null) | |
| { | |
| control.OnDecrease(); | |
| } | |
| } | |
| protected virtual void OnIncrease() | |
| { | |
| Value++; | |
| } | |
| protected virtual void OnDecrease() | |
| { | |
| Value--; | |
| } | |
| private static RoutedCommand _increaseCommand; | |
| private static RoutedCommand _decreaseCommand; | |
| //</SnippetCommands> | |
| #endregion | |
| private const decimal MinValue = 0, MaxValue = 100; | |
| } | |
| } |