|
| 1 | +using System.ComponentModel; |
| 2 | +using System.Globalization; |
| 3 | +using System.Windows.Automation.Peers; |
| 4 | + |
| 5 | +namespace MaterialDesignThemes.Wpf; |
| 6 | + |
| 7 | +[TemplatePart(Name = IncreaseButtonPartName, Type = typeof(RepeatButton))] |
| 8 | +[TemplatePart(Name = DecreaseButtonPartName, Type = typeof(RepeatButton))] |
| 9 | +[TemplatePart(Name = TextFieldBoxPartName, Type = typeof(TextBox))] |
| 10 | +public class NumericUpDown : Control |
| 11 | +{ |
| 12 | + public const string IncreaseButtonPartName = "PART_IncreaseButton"; |
| 13 | + public const string DecreaseButtonPartName = "PART_DecreaseButton"; |
| 14 | + public const string TextFieldBoxPartName = "PART_TextBoxField"; |
| 15 | + |
| 16 | + private TextBox? _textBoxField; |
| 17 | + private RepeatButton? _decreaseButton; |
| 18 | + private RepeatButton? _increaseButton; |
| 19 | + |
| 20 | + static NumericUpDown() |
| 21 | + { |
| 22 | + DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown))); |
| 23 | + } |
| 24 | + |
| 25 | + #region DependencyProperties |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + public object? IncreaseContent |
| 30 | + { |
| 31 | + get => GetValue(IncreaseContentProperty); |
| 32 | + set => SetValue(IncreaseContentProperty, value); |
| 33 | + } |
| 34 | + |
| 35 | + // Using a DependencyProperty as the backing store for IncreaseContent. This enables animation, styling, binding, etc... |
| 36 | + public static readonly DependencyProperty IncreaseContentProperty = |
| 37 | + DependencyProperty.Register(nameof(IncreaseContent), typeof(object), typeof(NumericUpDown), new PropertyMetadata(null)); |
| 38 | + |
| 39 | + public object? DecreaseContent |
| 40 | + { |
| 41 | + get => GetValue(DecreaseContentProperty); |
| 42 | + set => SetValue(DecreaseContentProperty, value); |
| 43 | + } |
| 44 | + |
| 45 | + // Using a DependencyProperty as the backing store for DecreaseContent. This enables animation, styling, binding, etc... |
| 46 | + public static readonly DependencyProperty DecreaseContentProperty = |
| 47 | + DependencyProperty.Register(nameof(DecreaseContent), typeof(object), typeof(NumericUpDown), new PropertyMetadata(null)); |
| 48 | + |
| 49 | + #region DependencyProperty : MinimumProperty |
| 50 | + public int Minimum |
| 51 | + { |
| 52 | + get => (int)GetValue(MinimumProperty); |
| 53 | + set => SetValue(MinimumProperty, value); |
| 54 | + } |
| 55 | + public static readonly DependencyProperty MinimumProperty = |
| 56 | + DependencyProperty.Register(nameof(Minimum), typeof(int), typeof(NumericUpDown), new PropertyMetadata(int.MinValue, OnMinimumChanged)); |
| 57 | + |
| 58 | + private static void OnMinimumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 59 | + { |
| 60 | + NumericUpDown ctrl = (NumericUpDown)d; |
| 61 | + ctrl.CoerceValue(ValueProperty); |
| 62 | + ctrl.CoerceValue(MaximumProperty); |
| 63 | + } |
| 64 | + |
| 65 | + #endregion DependencyProperty : MinimumProperty |
| 66 | + |
| 67 | + #region DependencyProperty : MaximumProperty |
| 68 | + |
| 69 | + public int Maximum |
| 70 | + { |
| 71 | + get => (int)GetValue(MaximumProperty); |
| 72 | + set => SetValue(MaximumProperty, value); |
| 73 | + } |
| 74 | + |
| 75 | + public static readonly DependencyProperty MaximumProperty = |
| 76 | + DependencyProperty.Register(nameof(Maximum), typeof(int), typeof(NumericUpDown), new PropertyMetadata(int.MaxValue, OnMaximumChanged, CoerceMaximum)); |
| 77 | + |
| 78 | + private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 79 | + { |
| 80 | + NumericUpDown ctrl = (NumericUpDown)d; |
| 81 | + ctrl.CoerceValue(ValueProperty); |
| 82 | + } |
| 83 | + |
| 84 | + private static object? CoerceMaximum(DependencyObject d, object? value) |
| 85 | + { |
| 86 | + if (d is NumericUpDown numericUpDown && |
| 87 | + value is int numericValue) |
| 88 | + { |
| 89 | + return Math.Max(numericUpDown.Minimum, numericValue); |
| 90 | + } |
| 91 | + return value; |
| 92 | + } |
| 93 | + |
| 94 | + #endregion DependencyProperty : MaximumProperty |
| 95 | + |
| 96 | + #region DependencyProperty : ValueProperty |
| 97 | + public int Value |
| 98 | + { |
| 99 | + get => (int)GetValue(ValueProperty); |
| 100 | + set => SetValue(ValueProperty, value); |
| 101 | + } |
| 102 | + |
| 103 | + public static readonly DependencyProperty ValueProperty = |
| 104 | + DependencyProperty.Register(nameof(Value), typeof(int), typeof(NumericUpDown), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNumericValueChanged, CoerceNumericValue)); |
| 105 | + |
| 106 | + private static void OnNumericValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 107 | + { |
| 108 | + if (d is NumericUpDown numericUpDown) |
| 109 | + { |
| 110 | + var args = new RoutedPropertyChangedEventArgs<int>((int)e.OldValue, (int)e.NewValue) |
| 111 | + { |
| 112 | + RoutedEvent = ValueChangedEvent |
| 113 | + }; |
| 114 | + numericUpDown.RaiseEvent(args); |
| 115 | + if (numericUpDown._textBoxField is { } textBox) |
| 116 | + { |
| 117 | + textBox.Text = ((int)e.NewValue).ToString(CultureInfo.CurrentUICulture); |
| 118 | + } |
| 119 | + |
| 120 | + if (numericUpDown._increaseButton is { } increaseButton) |
| 121 | + { |
| 122 | + increaseButton.IsEnabled = numericUpDown.Value != numericUpDown.Maximum; |
| 123 | + } |
| 124 | + |
| 125 | + if (numericUpDown._decreaseButton is { } decreaseButton) |
| 126 | + { |
| 127 | + decreaseButton.IsEnabled = numericUpDown.Value != numericUpDown.Minimum; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static object? CoerceNumericValue(DependencyObject d, object? value) |
| 133 | + { |
| 134 | + if (d is NumericUpDown numericUpDown && |
| 135 | + value is int numericValue) |
| 136 | + { |
| 137 | + numericValue = Math.Min(numericUpDown.Maximum, numericValue); |
| 138 | + numericValue = Math.Max(numericUpDown.Minimum, numericValue); |
| 139 | + return numericValue; |
| 140 | + } |
| 141 | + return value; |
| 142 | + } |
| 143 | + #endregion ValueProperty |
| 144 | + |
| 145 | + #region DependencyProperty : AllowChangeOnScroll |
| 146 | + |
| 147 | + public bool AllowChangeOnScroll |
| 148 | + { |
| 149 | + get => (bool)GetValue(AllowChangeOnScrollProperty); |
| 150 | + set => SetValue(AllowChangeOnScrollProperty, value); |
| 151 | + } |
| 152 | + |
| 153 | + public static readonly DependencyProperty AllowChangeOnScrollProperty = |
| 154 | + DependencyProperty.Register(nameof(AllowChangeOnScroll), typeof(bool), typeof(NumericUpDown), new PropertyMetadata(false)); |
| 155 | + |
| 156 | + #endregion |
| 157 | + |
| 158 | + #endregion DependencyProperties |
| 159 | + |
| 160 | + #region Event : ValueChangedEvent |
| 161 | + [Category("Behavior")] |
| 162 | + public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent(nameof(ValueChanged), RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<int>), typeof(NumericUpDown)); |
| 163 | + |
| 164 | + public event RoutedPropertyChangedEventHandler<int> ValueChanged |
| 165 | + { |
| 166 | + add => AddHandler(ValueChangedEvent, value); |
| 167 | + remove => RemoveHandler(ValueChangedEvent, value); |
| 168 | + } |
| 169 | + #endregion Event : ValueChangedEvent |
| 170 | + |
| 171 | + public override void OnApplyTemplate() |
| 172 | + { |
| 173 | + if (_increaseButton != null) |
| 174 | + _increaseButton.Click -= IncreaseButtonOnClick; |
| 175 | + |
| 176 | + if (_decreaseButton != null) |
| 177 | + _decreaseButton.Click -= DecreaseButtonOnClick; |
| 178 | + if (_textBoxField != null) |
| 179 | + _textBoxField.TextChanged -= OnTextBoxFocusLost; |
| 180 | + |
| 181 | + _increaseButton = GetTemplateChild(IncreaseButtonPartName) as RepeatButton; |
| 182 | + _decreaseButton = GetTemplateChild(DecreaseButtonPartName) as RepeatButton; |
| 183 | + _textBoxField = GetTemplateChild(TextFieldBoxPartName) as TextBox; |
| 184 | + |
| 185 | + if (_increaseButton != null) |
| 186 | + _increaseButton.Click += IncreaseButtonOnClick; |
| 187 | + |
| 188 | + if (_decreaseButton != null) |
| 189 | + _decreaseButton.Click += DecreaseButtonOnClick; |
| 190 | + |
| 191 | + if (_textBoxField != null) |
| 192 | + { |
| 193 | + _textBoxField.LostFocus += OnTextBoxFocusLost; |
| 194 | + _textBoxField.Text = Value.ToString(CultureInfo.CurrentUICulture); |
| 195 | + } |
| 196 | + |
| 197 | + base.OnApplyTemplate(); |
| 198 | + } |
| 199 | + |
| 200 | + private void OnTextBoxFocusLost(object sender, EventArgs e) |
| 201 | + { |
| 202 | + if (_textBoxField is { } textBoxField) |
| 203 | + { |
| 204 | + if (int.TryParse(textBoxField.Text, NumberStyles.Integer, CultureInfo.CurrentUICulture, out int numericValue)) |
| 205 | + { |
| 206 | + SetCurrentValue(ValueProperty, numericValue); |
| 207 | + } |
| 208 | + else |
| 209 | + { |
| 210 | + textBoxField.Text = Value.ToString(CultureInfo.CurrentUICulture); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + private void IncreaseButtonOnClick(object sender, RoutedEventArgs e) => OnIncrease(); |
| 216 | + |
| 217 | + private void DecreaseButtonOnClick(object sender, RoutedEventArgs e) => OnDecrease(); |
| 218 | + |
| 219 | + private void OnIncrease() |
| 220 | + { |
| 221 | + SetCurrentValue(ValueProperty, Value + 1); |
| 222 | + } |
| 223 | + |
| 224 | + private void OnDecrease() |
| 225 | + { |
| 226 | + SetCurrentValue(ValueProperty, Value - 1); |
| 227 | + } |
| 228 | + |
| 229 | + protected override void OnPreviewKeyDown(KeyEventArgs e) |
| 230 | + { |
| 231 | + if (e.Key == Key.Up) |
| 232 | + { |
| 233 | + OnIncrease(); |
| 234 | + e.Handled = true; |
| 235 | + } |
| 236 | + else if (e.Key == Key.Down) |
| 237 | + { |
| 238 | + OnDecrease(); |
| 239 | + e.Handled = true; |
| 240 | + } |
| 241 | + base.OnPreviewKeyDown(e); |
| 242 | + } |
| 243 | + |
| 244 | + protected override void OnPreviewMouseWheel(MouseWheelEventArgs e) |
| 245 | + { |
| 246 | + if (IsKeyboardFocusWithin && AllowChangeOnScroll) |
| 247 | + { |
| 248 | + if (e.Delta > 0) |
| 249 | + { |
| 250 | + OnIncrease(); |
| 251 | + } |
| 252 | + else if (e.Delta < 0) |
| 253 | + { |
| 254 | + OnDecrease(); |
| 255 | + } |
| 256 | + e.Handled = true; |
| 257 | + } |
| 258 | + base.OnPreviewMouseWheel(e); |
| 259 | + } |
| 260 | +} |
0 commit comments