1
- using System . ComponentModel ;
1
+ using System . ComponentModel ;
2
+ using System . Globalization ;
3
+ using System . Runtime . CompilerServices ;
2
4
3
5
namespace MaterialDesignThemes . Wpf ;
4
6
@@ -46,10 +48,38 @@ public int Maximum
46
48
public int Value
47
49
{
48
50
get => ( int ) GetValue ( ValueProperty ) ;
49
- set => SetValue ( ValueProperty , ValidateInput ( value , nameof ( Value ) ) ) ;
51
+ set => SetValue ( ValueProperty , value ) ;
50
52
}
51
53
public static readonly DependencyProperty ValueProperty =
52
- DependencyProperty . Register ( nameof ( Value ) , typeof ( int ) , typeof ( NumericUpDown ) , new PropertyMetadata ( 0 , OnValueChanged ) ) ;
54
+ DependencyProperty . Register ( nameof ( Value ) , typeof ( int ) , typeof ( NumericUpDown ) , new FrameworkPropertyMetadata ( 0 , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault , OnNumericValueChanged , CoerceNumericValue ) ) ;
55
+
56
+ private static void OnNumericValueChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
57
+ {
58
+ if ( d is NumericUpDown numericUpDown )
59
+ {
60
+ var args = new RoutedPropertyChangedEventArgs < int > ( ( int ) e . OldValue , ( int ) e . NewValue )
61
+ {
62
+ RoutedEvent = ValueChangedEvent
63
+ } ;
64
+ numericUpDown . RaiseEvent ( args ) ;
65
+ if ( numericUpDown . _textBoxField is { } textBox )
66
+ {
67
+ textBox . Text = ( ( int ) e . NewValue ) . ToString ( CultureInfo . CurrentUICulture ) ;
68
+ }
69
+ }
70
+ }
71
+
72
+ private static object CoerceNumericValue ( DependencyObject d , object value )
73
+ {
74
+ if ( d is NumericUpDown numericUpDown &&
75
+ value is int numericValue )
76
+ {
77
+ numericValue = Math . Min ( numericUpDown . Maximum , numericValue ) ;
78
+ numericValue = Math . Max ( numericUpDown . Minimum , numericValue ) ;
79
+ return numericValue ;
80
+ }
81
+ return value ;
82
+ }
53
83
#endregion ValueProperty
54
84
55
85
#region DependencyProperty : IncreaseCommandProperty
@@ -72,7 +102,7 @@ public ICommand? DecreaseCommand
72
102
DependencyProperty . Register ( nameof ( DecreaseCommand ) , typeof ( ICommand ) , typeof ( NumericUpDown ) , new PropertyMetadata ( default ( ICommand ? ) ) ) ;
73
103
#endregion DependencyProperty : DecreaseCommandProperty
74
104
75
- #endregion DependencyProperty : DependencyProperties
105
+ #endregion DependencyProperties
76
106
77
107
#region Event : ValueChangedEvent
78
108
[ Category ( "Behavior" ) ]
@@ -85,35 +115,13 @@ public event RoutedPropertyChangedEventHandler<int> ValueChanged
85
115
}
86
116
#endregion Event : ValueChangedEvent
87
117
88
- private static void OnValueChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
89
- {
90
- var control = d as NumericUpDown ;
91
- var args = new RoutedPropertyChangedEventArgs < int > ( ( int ) e . OldValue , ( int ) e . NewValue )
92
- {
93
- RoutedEvent = ValueChangedEvent
94
- } ;
95
- control ? . RaiseEvent ( args ) ;
96
- }
97
-
98
- private object ValidateInput ( object input , string propertyName = "Value" )
99
- {
100
- if ( ! int . TryParse ( input . ToString ( ) , out int value ) )
101
- return Value ;
102
-
103
- value = Math . Min ( Maximum , value ) ;
104
- value = Math . Max ( Minimum , value ) ;
105
-
106
- return value ;
107
- }
108
-
109
118
public override void OnApplyTemplate ( )
110
119
{
111
120
if ( _increaseButton != null )
112
- _increaseButton ! . Click -= IncreaseButtonOnClick ;
121
+ _increaseButton . Click -= IncreaseButtonOnClick ;
113
122
114
123
if ( _decreaseButton != null )
115
- _decreaseButton ! . Click -= DecreaseButtonOnClick ;
116
-
124
+ _decreaseButton . Click -= DecreaseButtonOnClick ;
117
125
if ( _textBoxField != null )
118
126
_textBoxField ! . TextChanged -= OnTextBoxFocusLost ;
119
127
@@ -122,72 +130,52 @@ public override void OnApplyTemplate()
122
130
_textBoxField = GetTemplateChild ( TextFieldBoxPartName ) as TextBox ;
123
131
124
132
if ( _increaseButton != null )
125
- _increaseButton ! . Click += IncreaseButtonOnClick ;
133
+ _increaseButton . Click += IncreaseButtonOnClick ;
126
134
127
135
if ( _decreaseButton != null )
128
136
_decreaseButton ! . Click += DecreaseButtonOnClick ;
129
137
130
138
if ( _textBoxField != null )
131
139
{
132
140
_textBoxField ! . LostFocus += OnTextBoxFocusLost ;
133
- _textBoxField . Text = Value . ToString ( ) ;
141
+ _textBoxField . Text = Value . ToString ( CultureInfo . CurrentUICulture ) ;
134
142
}
135
143
136
- ValueChanged += NumericUpDown_ValueChanged ;
137
-
138
144
base . OnApplyTemplate ( ) ;
139
145
}
140
146
141
- private void NumericUpDown_ValueChanged ( object sender , RoutedPropertyChangedEventArgs < int > e )
142
- {
143
- if ( sender == null ) return ;
144
- if ( sender is not NumericUpDown ) return ;
145
-
146
- var tBox = ( sender ! as NumericUpDown ) ! . _textBoxField ;
147
- if ( tBox == null ) return ;
148
-
149
- tBox . Text = Value . ToString ( ) ;
150
-
151
- }
152
-
153
147
private void OnTextBoxFocusLost ( object sender , EventArgs e )
154
148
{
155
- if ( sender == null ) return ;
156
-
157
- TextBox ? tbox = sender as TextBox ;
158
- if ( tbox == null ) return ;
149
+ if ( int . TryParse ( _textBoxField ? . Text , NumberStyles . Integer , CultureInfo . CurrentUICulture , out int numericValue ) )
150
+ {
151
+ SetCurrentValue ( ValueProperty , numericValue ) ;
152
+ }
159
153
160
- Value = ( int ) ValidateInput ( tbox . Text ) ;
161
154
}
162
-
163
155
private void IncreaseButtonOnClick ( object sender , RoutedEventArgs e )
164
156
{
165
157
OnIncrease ( ) ;
166
- e . Handled = true ;
167
158
}
168
159
169
160
private void DecreaseButtonOnClick ( object sender , RoutedEventArgs e )
170
161
{
171
162
OnDecrease ( ) ;
172
- e . Handled = true ;
173
163
}
174
164
175
165
private void OnIncrease ( )
176
166
{
177
- if ( IncreaseCommand ? . CanExecute ( this ) ?? false )
178
- IncreaseCommand . Execute ( this ) ;
167
+ SetCurrentValue ( ValueProperty , Value + 1 ) ;
179
168
180
- Value += 1 ;
181
- if ( Value > Maximum ) Value = Maximum ;
169
+ if ( IncreaseCommand ? . CanExecute ( null ) ?? false )
170
+ IncreaseCommand . Execute ( null ) ;
182
171
}
183
172
184
173
private void OnDecrease ( )
185
174
{
175
+ SetCurrentValue ( ValueProperty , Value - 1 ) ;
176
+
186
177
if ( DecreaseCommand ? . CanExecute ( this ) ?? false )
187
178
DecreaseCommand . Execute ( this ) ;
188
-
189
- Value -= 1 ;
190
- if ( Value < Minimum ) Value = Minimum ;
191
179
}
192
180
193
181
protected override void OnPreviewKeyDown ( KeyEventArgs e )
@@ -207,15 +195,18 @@ protected override void OnPreviewKeyDown(KeyEventArgs e)
207
195
208
196
protected override void OnPreviewMouseWheel ( MouseWheelEventArgs e )
209
197
{
210
- if ( e . Delta > 0 )
211
- {
212
- OnIncrease ( ) ;
213
- e . Handled = true ;
214
- }
215
- else if ( e . Delta < 0 )
198
+ if ( IsKeyboardFocusWithin )
216
199
{
217
- OnDecrease ( ) ;
218
- e . Handled = true ;
200
+ if ( e . Delta > 0 )
201
+ {
202
+ OnIncrease ( ) ;
203
+ e . Handled = true ;
204
+ }
205
+ else if ( e . Delta < 0 )
206
+ {
207
+ OnDecrease ( ) ;
208
+ e . Handled = true ;
209
+ }
219
210
}
220
211
base . OnPreviewMouseWheel ( e ) ;
221
212
}
0 commit comments