Skip to content

Commit a80b199

Browse files
committed
fix: #4545 update up and down button states on manual input
1 parent aefb1e0 commit a80b199

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

src/Directory.packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="8.0.1" />
3030
<PackageVersion Include="System.Memory" Version="4.6.2" />
3131
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.1" Condition="'$(TargetFramework)' != 'net8.0-windows'" />
32+
<PackageVersion Include="System.ValueTuple" Version="4.6.1" Condition="$(DefineConstants.Contains(NETCOREAPP)) == false" />
3233

3334
<PackageVersion Include="WpfAnalyzers" Version="4.1.1" />
3435
</ItemGroup>

src/MahApps.Metro/Controls/NumericUpDown.cs

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public static readonly DependencyProperty InterceptArrowKeysProperty
307307
new FrameworkPropertyMetadata(BooleanBoxes.TrueBox));
308308

309309
/// <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.
311311
/// </summary>
312312
[Bindable(true)]
313313
[Category("Behavior")]
@@ -373,7 +373,7 @@ public static readonly DependencyProperty ValueProperty
373373
new FrameworkPropertyMetadata(default(double?),
374374
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
375375
OnValuePropertyChanged,
376-
(o, value) => CoerceValue(o, value).Item1));
376+
(o, value) => CoerceValue(o, value).value));
377377

378378
private static void OnValuePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
379379
{
@@ -384,12 +384,12 @@ private static void OnValuePropertyChanged(DependencyObject dependencyObject, De
384384
}
385385

386386
[MustUseReturnValue]
387-
private static Tuple<double?, bool> CoerceValue(DependencyObject d, object? baseValue)
387+
private static (double? value, bool isValid) CoerceValue(DependencyObject d, object? baseValue)
388388
{
389389
var numericUpDown = (NumericUpDown)d;
390390
if (baseValue is null)
391391
{
392-
return new Tuple<double?, bool>(numericUpDown.DefaultValue, false);
392+
return (numericUpDown.DefaultValue, false);
393393
}
394394

395395
var value = ((double?)baseValue).Value;
@@ -401,15 +401,15 @@ private static void OnValuePropertyChanged(DependencyObject dependencyObject, De
401401

402402
if (value < numericUpDown.Minimum)
403403
{
404-
return new Tuple<double?, bool>(numericUpDown.Minimum, true);
404+
return (numericUpDown.Minimum, false);
405405
}
406406

407407
if (value > numericUpDown.Maximum)
408408
{
409-
return new Tuple<double?, bool>(numericUpDown.Maximum, true);
409+
return (numericUpDown.Maximum, false);
410410
}
411411

412-
return new Tuple<double?, bool>(value, false);
412+
return (value, true);
413413
}
414414

415415
/// <summary>
@@ -757,7 +757,7 @@ public static readonly DependencyProperty ButtonUpContentStringFormatProperty
757757
/// <summary>
758758
/// Gets or sets a composite string that specifies how to format the ButtonUpContent property if it is displayed as a string.
759759
/// </summary>
760-
/// <remarks>
760+
/// <remarks>
761761
/// This property is ignored if <seealso cref="ButtonUpContentTemplate"/> is set.
762762
/// </remarks>
763763
[Bindable(true)]
@@ -811,7 +811,7 @@ public static readonly DependencyProperty ButtonDownContentStringFormatProperty
811811
/// <summary>
812812
/// Gets or sets a composite string that specifies how to format the ButtonDownContent property if it is displayed as a string.
813813
/// </summary>
814-
/// <remarks>
814+
/// <remarks>
815815
/// This property is ignored if <seealso cref="ButtonDownContentTemplate"/> is set.
816816
/// </remarks>
817817
[Bindable(true)]
@@ -960,7 +960,7 @@ static NumericUpDown()
960960
EventManager.RegisterClassHandler(typeof(NumericUpDown), GotFocusEvent, new RoutedEventHandler(OnGotFocus));
961961
}
962962

963-
/// <summary>
963+
/// <summary>
964964
/// Called when this element or any below gets focus.
965965
/// </summary>
966966
private static void OnGotFocus(object sender, RoutedEventArgs e)
@@ -1171,7 +1171,8 @@ protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
11711171
var fullText = textBox.Text.Remove(textBox.SelectionStart, textBox.SelectionLength).Insert(textBox.CaretIndex, e.Text);
11721172
var textIsValid = this.ValidateText(fullText, out var convertedValue);
11731173
// 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;
11751176
this.manualChange = !e.Handled;
11761177
}
11771178

@@ -1195,6 +1196,8 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
11951196
this.valueTextBox.Text = null;
11961197
}
11971198

1199+
this.EnableDisableUpDown();
1200+
11981201
if (oldValue != newValue)
11991202
{
12001203
this.RaiseEvent(new RoutedPropertyChangedEventArgs<double?>(oldValue, newValue, ValueChangedEvent));
@@ -1203,22 +1206,12 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
12031206
return;
12041207
}
12051208

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);
12151211

12161212
if (newValue <= this.Minimum)
12171213
{
1218-
if (this.repeatDown != null)
1219-
{
1220-
this.repeatDown.IsEnabled = false;
1221-
}
1214+
this.repeatDown?.SetCurrentValue(RepeatButton.IsEnabledProperty, BooleanBoxes.FalseBox);
12221215

12231216
this.ResetInternal();
12241217

@@ -1230,12 +1223,10 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
12301223

12311224
if (newValue >= this.Maximum)
12321225
{
1233-
if (this.repeatUp != null)
1234-
{
1235-
this.repeatUp.IsEnabled = false;
1236-
}
1226+
this.repeatUp?.SetCurrentValue(RepeatButton.IsEnabledProperty, BooleanBoxes.FalseBox);
12371227

12381228
this.ResetInternal();
1229+
12391230
if (this.IsLoaded)
12401231
{
12411232
this.RaiseEvent(new RoutedEventArgs(MaximumReachedEvent));
@@ -1248,6 +1239,8 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue)
12481239
}
12491240
}
12501241

1242+
this.EnableDisableUpDown();
1243+
12511244
if (oldValue != newValue)
12521245
{
12531246
this.RaiseEvent(new RoutedPropertyChangedEventArgs<double?>(oldValue, newValue, ValueChangedEvent));
@@ -1457,29 +1450,13 @@ private void SetValueTo(double newValue)
14571450
value = this.Minimum;
14581451
}
14591452

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);
14771454
}
14781455

14791456
private void EnableDisableUpDown()
14801457
{
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));
14831460
}
14841461

14851462
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
@@ -1578,6 +1555,8 @@ private void ChangeValueFromTextInput(string text)
15781555
return;
15791556
}
15801557

1558+
var oldValue = this.Value;
1559+
15811560
if (string.IsNullOrEmpty(text))
15821561
{
15831562
if (this.DefaultValue.HasValue)
@@ -1603,15 +1582,15 @@ private void ChangeValueFromTextInput(string text)
16031582
else if (this.DefaultValue.HasValue)
16041583
{
16051584
this.SetValueTo(this.DefaultValue.Value);
1606-
this.InternalSetText(this.Value);
1585+
this.InternalSetText(oldValue);
16071586
}
16081587
else
16091588
{
16101589
this.SetCurrentValue(ValueProperty, null);
16111590
}
16121591
}
16131592

1614-
this.OnValueChanged(this.Value, this.Value);
1593+
this.OnValueChanged(oldValue, this.Value);
16151594

16161595
this.manualChange = false;
16171596
}

src/MahApps.Metro/MahApps.Metro.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<ItemGroup Condition="$(DefineConstants.Contains(NETCOREAPP)) == false">
4545
<Reference Include="System.ComponentModel.DataAnnotations" />
4646
<Reference Include="System.Configuration" />
47+
<PackageReference Include="System.ValueTuple" />
4748
</ItemGroup>
4849

4950
<!-- Items include -->

0 commit comments

Comments
 (0)