@@ -307,7 +307,7 @@ public static readonly DependencyProperty InterceptArrowKeysProperty
307
307
new FrameworkPropertyMetadata ( BooleanBoxes . TrueBox ) ) ;
308
308
309
309
/// <summary>
310
- /// Gets or sets a value indicating whether the user can use the arrow keys <see cref="Key.Up"/> and <see cref="Key.Down"/> to change the value.
310
+ /// Gets or sets a value indicating whether the user can use the arrow keys <see cref="Key.Up"/> and <see cref="Key.Down"/> to change the value.
311
311
/// </summary>
312
312
[ Bindable ( true ) ]
313
313
[ Category ( "Behavior" ) ]
@@ -373,7 +373,7 @@ public static readonly DependencyProperty ValueProperty
373
373
new FrameworkPropertyMetadata ( default ( double ? ) ,
374
374
FrameworkPropertyMetadataOptions . BindsTwoWayByDefault ,
375
375
OnValuePropertyChanged ,
376
- ( o , value ) => CoerceValue ( o , value ) . Item1 ) ) ;
376
+ ( o , value ) => CoerceValue ( o , value ) . value ) ) ;
377
377
378
378
private static void OnValuePropertyChanged ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs e )
379
379
{
@@ -384,12 +384,12 @@ private static void OnValuePropertyChanged(DependencyObject dependencyObject, De
384
384
}
385
385
386
386
[ MustUseReturnValue ]
387
- private static Tuple < double ? , bool > CoerceValue ( DependencyObject d , object ? baseValue )
387
+ private static ( double ? value , bool isValid ) CoerceValue ( DependencyObject d , object ? baseValue )
388
388
{
389
389
var numericUpDown = ( NumericUpDown ) d ;
390
390
if ( baseValue is null )
391
391
{
392
- return new Tuple < double ? , bool > ( numericUpDown . DefaultValue , false ) ;
392
+ return ( numericUpDown . DefaultValue , false ) ;
393
393
}
394
394
395
395
var value = ( ( double ? ) baseValue ) . Value ;
@@ -401,15 +401,15 @@ private static void OnValuePropertyChanged(DependencyObject dependencyObject, De
401
401
402
402
if ( value < numericUpDown . Minimum )
403
403
{
404
- return new Tuple < double ? , bool > ( numericUpDown . Minimum , true ) ;
404
+ return ( numericUpDown . Minimum , false ) ;
405
405
}
406
406
407
407
if ( value > numericUpDown . Maximum )
408
408
{
409
- return new Tuple < double ? , bool > ( numericUpDown . Maximum , true ) ;
409
+ return ( numericUpDown . Maximum , false ) ;
410
410
}
411
411
412
- return new Tuple < double ? , bool > ( value , false ) ;
412
+ return ( value , true ) ;
413
413
}
414
414
415
415
/// <summary>
@@ -757,7 +757,7 @@ public static readonly DependencyProperty ButtonUpContentStringFormatProperty
757
757
/// <summary>
758
758
/// Gets or sets a composite string that specifies how to format the ButtonUpContent property if it is displayed as a string.
759
759
/// </summary>
760
- /// <remarks>
760
+ /// <remarks>
761
761
/// This property is ignored if <seealso cref="ButtonUpContentTemplate"/> is set.
762
762
/// </remarks>
763
763
[ Bindable ( true ) ]
@@ -811,7 +811,7 @@ public static readonly DependencyProperty ButtonDownContentStringFormatProperty
811
811
/// <summary>
812
812
/// Gets or sets a composite string that specifies how to format the ButtonDownContent property if it is displayed as a string.
813
813
/// </summary>
814
- /// <remarks>
814
+ /// <remarks>
815
815
/// This property is ignored if <seealso cref="ButtonDownContentTemplate"/> is set.
816
816
/// </remarks>
817
817
[ Bindable ( true ) ]
@@ -960,7 +960,7 @@ static NumericUpDown()
960
960
EventManager . RegisterClassHandler ( typeof ( NumericUpDown ) , GotFocusEvent , new RoutedEventHandler ( OnGotFocus ) ) ;
961
961
}
962
962
963
- /// <summary>
963
+ /// <summary>
964
964
/// Called when this element or any below gets focus.
965
965
/// </summary>
966
966
private static void OnGotFocus ( object sender , RoutedEventArgs e )
@@ -1171,7 +1171,8 @@ protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
1171
1171
var fullText = textBox . Text . Remove ( textBox . SelectionStart , textBox . SelectionLength ) . Insert ( textBox . CaretIndex , e . Text ) ;
1172
1172
var textIsValid = this . ValidateText ( fullText , out var convertedValue ) ;
1173
1173
// Value must be valid and not coerced
1174
- e . Handled = ! textIsValid || CoerceValue ( this , convertedValue as double ? ) . Item2 ;
1174
+ var coerceValue = CoerceValue ( this , convertedValue as double ? ) ;
1175
+ e . Handled = ! textIsValid || ! coerceValue . isValid ;
1175
1176
this . manualChange = ! e . Handled ;
1176
1177
}
1177
1178
@@ -1195,6 +1196,8 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
1195
1196
this . valueTextBox . Text = null ;
1196
1197
}
1197
1198
1199
+ this . EnableDisableUpDown ( ) ;
1200
+
1198
1201
if ( oldValue != newValue )
1199
1202
{
1200
1203
this . RaiseEvent ( new RoutedPropertyChangedEventArgs < double ? > ( oldValue , newValue , ValueChangedEvent ) ) ;
@@ -1203,22 +1206,12 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
1203
1206
return ;
1204
1207
}
1205
1208
1206
- if ( this . repeatUp != null && ! this . repeatUp . IsEnabled )
1207
- {
1208
- this . repeatUp . IsEnabled = true ;
1209
- }
1210
-
1211
- if ( this . repeatDown != null && ! this . repeatDown . IsEnabled )
1212
- {
1213
- this . repeatDown . IsEnabled = true ;
1214
- }
1209
+ this . repeatUp ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . TrueBox ) ;
1210
+ this . repeatDown ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . TrueBox ) ;
1215
1211
1216
1212
if ( newValue <= this . Minimum )
1217
1213
{
1218
- if ( this . repeatDown != null )
1219
- {
1220
- this . repeatDown . IsEnabled = false ;
1221
- }
1214
+ this . repeatDown ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . FalseBox ) ;
1222
1215
1223
1216
this . ResetInternal ( ) ;
1224
1217
@@ -1230,12 +1223,10 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
1230
1223
1231
1224
if ( newValue >= this . Maximum )
1232
1225
{
1233
- if ( this . repeatUp != null )
1234
- {
1235
- this . repeatUp . IsEnabled = false ;
1236
- }
1226
+ this . repeatUp ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . FalseBox ) ;
1237
1227
1238
1228
this . ResetInternal ( ) ;
1229
+
1239
1230
if ( this . IsLoaded )
1240
1231
{
1241
1232
this . RaiseEvent ( new RoutedEventArgs ( MaximumReachedEvent ) ) ;
@@ -1248,6 +1239,8 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
1248
1239
}
1249
1240
}
1250
1241
1242
+ this . EnableDisableUpDown ( ) ;
1243
+
1251
1244
if ( oldValue != newValue )
1252
1245
{
1253
1246
this . RaiseEvent ( new RoutedPropertyChangedEventArgs < double ? > ( oldValue , newValue , ValueChangedEvent ) ) ;
@@ -1457,29 +1450,13 @@ private void SetValueTo(double newValue)
1457
1450
value = this . Minimum ;
1458
1451
}
1459
1452
1460
- this . SetCurrentValue ( ValueProperty , CoerceValue ( this , value ) . Item1 ) ;
1461
- }
1462
-
1463
- private void EnableDisableDown ( )
1464
- {
1465
- if ( this . repeatDown != null )
1466
- {
1467
- this . repeatDown . IsEnabled = this . Value is null || this . Value > this . Minimum ;
1468
- }
1469
- }
1470
-
1471
- private void EnableDisableUp ( )
1472
- {
1473
- if ( this . repeatUp != null )
1474
- {
1475
- this . repeatUp . IsEnabled = this . Value is null || this . Value < this . Maximum ;
1476
- }
1453
+ this . SetCurrentValue ( ValueProperty , CoerceValue ( this , value ) . value ) ;
1477
1454
}
1478
1455
1479
1456
private void EnableDisableUpDown ( )
1480
1457
{
1481
- this . EnableDisableUp ( ) ;
1482
- this . EnableDisableDown ( ) ;
1458
+ this . repeatUp ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . Box ( this . Value is null || this . Value < this . Maximum ) ) ;
1459
+ this . repeatDown ? . SetCurrentValue ( RepeatButton . IsEnabledProperty , BooleanBoxes . Box ( this . Value is null || this . Value > this . Minimum ) ) ;
1483
1460
}
1484
1461
1485
1462
private void OnTextBoxKeyDown ( object sender , KeyEventArgs e )
@@ -1578,6 +1555,8 @@ private void ChangeValueFromTextInput(string text)
1578
1555
return ;
1579
1556
}
1580
1557
1558
+ var oldValue = this . Value ;
1559
+
1581
1560
if ( string . IsNullOrEmpty ( text ) )
1582
1561
{
1583
1562
if ( this . DefaultValue . HasValue )
@@ -1603,15 +1582,15 @@ private void ChangeValueFromTextInput(string text)
1603
1582
else if ( this . DefaultValue . HasValue )
1604
1583
{
1605
1584
this . SetValueTo ( this . DefaultValue . Value ) ;
1606
- this . InternalSetText ( this . Value ) ;
1585
+ this . InternalSetText ( oldValue ) ;
1607
1586
}
1608
1587
else
1609
1588
{
1610
1589
this . SetCurrentValue ( ValueProperty , null ) ;
1611
1590
}
1612
1591
}
1613
1592
1614
- this . OnValueChanged ( this . Value , this . Value ) ;
1593
+ this . OnValueChanged ( oldValue , this . Value ) ;
1615
1594
1616
1595
this . manualChange = false ;
1617
1596
}
0 commit comments