added samples
[windows-sources.git] / sdk / samples / WPFSamples / UserControlNumericUpDown / csharp / numericupdown.xaml.cs
blob9be4a831125d0315fcdf4c5f4654991bc85d49f2
1 //<SnippetCodeBehind>
2 using System;
3 using System.Windows;
4 using System.Windows.Controls;
6 namespace MyUserControl
8 public partial class NumericUpDown : System.Windows.Controls.UserControl
10 /// <summary>
11 /// Initializes a new instance of the NumericUpDownControl.
12 /// </summary>
13 public NumericUpDown()
15 InitializeComponent();
19 /// <summary>
20 /// Identifies the Value dependency property.
21 /// </summary>
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)));
28 /// <summary>
29 /// Gets or sets the value assigned to the control.
30 /// </summary>
31 public decimal Value
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));
44 return 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);
56 /// <summary>
57 /// Identifies the ValueChanged routed event.
58 /// </summary>
59 public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(
60 "ValueChanged", RoutingStrategy.Bubble,
61 typeof(RoutedPropertyChangedEventHandler<decimal>), typeof(NumericUpDown));
63 /// <summary>
64 /// Occurs when the Value property changes.
65 /// </summary>
66 public event RoutedPropertyChangedEventHandler<decimal> ValueChanged
68 add { AddHandler(ValueChangedEvent, value); }
69 remove { RemoveHandler(ValueChangedEvent, value); }
72 /// <summary>
73 /// Raises the ValueChanged event.
74 /// </summary>
75 /// <param name="args">Arguments associated with the ValueChanged event.</param>
76 protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<decimal> args)
78 RaiseEvent(args);
81 private void upButton_Click(object sender, EventArgs e)
83 Value++;
86 private void downButton_Click(object sender, EventArgs e)
88 Value--;
91 private const decimal MinValue = 0, MaxValue = 100;
94 //</SnippetCodeBehind>