Skip to content

Commit 7f56dc7

Browse files
authored
``` (#4064)
fix: Prevent NumericUpDown from resetting text input during typing ``` ``` Introduces a flag to temporarily disable the text box's value update while the user is actively typing. This prevents the control from prematurely overwriting partial user input, which previously made it difficult to enter values when a minimum or maximum was set. Resolves #4061 ```
1 parent 836fcc5 commit 7f56dc7

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/MaterialDesignThemes.Wpf/UpDownBase.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ private static bool TryParse(string text, IFormatProvider? formatProvider, [NotN
4747
private static int Compare(T value1, T value2) => _arithmetic.Compare(value1, value2);
4848
#endif
4949

50+
private bool _isTextChanging;
51+
5052
#region DependencyProperties
5153

5254
#region DependencyProperty : MinimumProperty
@@ -116,7 +118,7 @@ private static void OnNumericValueChanged(DependencyObject d, DependencyProperty
116118
};
117119
upDownBase.RaiseEvent(args);
118120

119-
if (upDownBase._textBoxField is { } textBox)
121+
if (!upDownBase._isTextChanging && upDownBase._textBoxField is { } textBox)
120122
{
121123
textBox.Text = Convert.ToString(e.NewValue, CultureInfo.CurrentCulture);
122124
}
@@ -196,10 +198,13 @@ public event RoutedPropertyChangedEventHandler<T> ValueChanged
196198
public override void OnApplyTemplate()
197199
{
198200
if (_increaseButton != null)
201+
{
199202
_increaseButton.Click -= IncreaseButtonOnClick;
200-
203+
}
201204
if (_decreaseButton != null)
205+
{
202206
_decreaseButton.Click -= DecreaseButtonOnClick;
207+
}
203208
if (_textBoxField != null)
204209
{
205210
_textBoxField.TextChanged -= OnTextBoxTextChanged;
@@ -243,7 +248,9 @@ private void OnTextBoxTextChanged(object sender, EventArgs e)
243248
{
244249
if (TryParse(textBoxField.Text, CultureInfo.CurrentCulture, out T? value))
245250
{
251+
_isTextChanging = true;
246252
SetCurrentValue(ValueProperty, ClampValue(value));
253+
_isTextChanging = false;
247254
}
248255
}
249256
}

tests/MaterialDesignThemes.UITests/WPF/UpDownControls/NumericUpDownTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,30 @@ public async Task NumericUpDown_WhenBindingUpdateTriggerIsPropertyChanged_ItUpda
213213
await Assert.That(tag?.ToString()).IsEqualTo("4");
214214
}
215215

216+
[Test]
217+
[Description("Issue 4061")]
218+
public async Task NumericUpDown_WhenMinimumIsPositiveNumber_ItAllowsTextToBeChangedWithoutClamping()
219+
{
220+
//Arrange
221+
//NB: Value should be something that is not equal to the minimum
222+
var numericUpDown = await LoadXaml<NumericUpDown>("""
223+
<materialDesign:NumericUpDown Tag="{Binding Value, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" Maximum="150" Minimum="60" Value="70" />
224+
""");
225+
226+
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
227+
228+
//Act
229+
await textBox.MoveKeyboardFocus();
230+
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}8");
231+
object? firstValue = await numericUpDown.GetTag();
232+
await textBox.SendKeyboardInput($"7");
233+
object? secondValue = await numericUpDown.GetTag();
234+
235+
//Assert
236+
await Assert.That(firstValue?.ToString()).IsEqualTo("60");
237+
await Assert.That(secondValue?.ToString()).IsEqualTo("87");
238+
}
239+
216240
[Test]
217241
[Description("Issue 3827")]
218242
public async Task NumericUpDown_WhenBindingUpdateTriggerIsLostFocus_ItDoesNotUpdateUntilItLoosesFocus()

0 commit comments

Comments
 (0)