Skip to content

Commit ee04080

Browse files
corvinszKeboo
andauthored
Fix 3780 - NumericUpDown/DecimalUpDown display does not update with values ​​outside the range (#3781)
* possibily fixed 3780, adjusted the demo app to play around and reproduce the bug * cleanup demo * Adding UI tests Removed the direct Coerce calls as it will be handled by the DependencyProperty --------- Co-authored-by: Kevin Bost <[email protected]>
1 parent d82fb55 commit ee04080

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

src/MaterialDesignThemes.Wpf/UpDownBase.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,8 @@ private void OnTextBoxFocusLost(object sender, EventArgs e)
215215
{
216216
SetCurrentValue(ValueProperty, value);
217217
}
218-
else
219-
{
220-
textBoxField.Text = Value?.ToString();
221-
}
218+
//NB: Because setting ValueProperty will coerce the value, we re-assign back to the textbox here.
219+
textBoxField.Text = Value?.ToString();
222220
}
223221
}
224222

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ await Wait.For(async () =>
3232
Assert.Equal("1", await textBox.GetText());
3333
Assert.Equal(1, await numericUpDown.GetValue());
3434
});
35+
36+
recorder.Success();
3537
}
3638

3739
[Fact]
@@ -63,6 +65,8 @@ await Wait.For(async () =>
6365
});
6466

6567
Assert.True(await plusButton.GetIsEnabled());
68+
69+
recorder.Success();
6670
}
6771

6872
[Fact]
@@ -94,6 +98,8 @@ await Wait.For(async () =>
9498
});
9599

96100
Assert.True(await minusButton.GetIsEnabled());
101+
102+
recorder.Success();
97103
}
98104

99105
[Fact]
@@ -116,6 +122,8 @@ public async Task MaxAndMinAssignments_CoerceValueToBeInRange()
116122
Assert.Equal(3, await numericUpDown.GetValue());
117123
Assert.Equal(3, await numericUpDown.GetMinimum());
118124
Assert.Equal(3, await numericUpDown.GetMaximum());
125+
126+
recorder.Success();
119127
}
120128

121129
[Fact]
@@ -147,4 +155,63 @@ public async Task InternalTextBoxIsFocused_WhenGettingKeyboardFocus()
147155

148156
recorder.Success();
149157
}
158+
159+
[Fact]
160+
[Description("Issue 3781")]
161+
public async Task IncreaseButtonClickWhenTextIsAboveMaximum_DoesNotIncreaseValue()
162+
{
163+
await using var recorder = new TestRecorder(App);
164+
165+
var stackPanel = await LoadXaml<StackPanel>("""
166+
<StackPanel>
167+
<materialDesign:DecimalUpDown Minimum="-2.5" Maximum="2.5" Value="2.5" />
168+
<Button Content="AlternateFocusElement" />
169+
</StackPanel>
170+
""");
171+
var decimalUpDown = await stackPanel.GetElement<DecimalUpDown>();
172+
var textBox = await decimalUpDown.GetElement<TextBox>("PART_TextBox");
173+
var plusButton = await decimalUpDown.GetElement<RepeatButton>("PART_IncreaseButton");
174+
175+
var button = await stackPanel.GetElement<Button>();
176+
177+
await textBox.MoveKeyboardFocus();
178+
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}30");
179+
await plusButton.LeftClick();
180+
181+
//NB: Because the focus has not left the up down control, we don't expect the text to change
182+
Assert.Equal("30", await textBox.GetText());
183+
Assert.Equal(2.5m, await decimalUpDown.GetValue());
184+
185+
recorder.Success();
186+
}
187+
188+
[Theory]
189+
[Description("Issue 3781")]
190+
[InlineData("30")]
191+
[InlineData("abc")]
192+
[InlineData("2a")]
193+
public async Task LostFocusWhenTextIsInvalid_RevertsToOriginalValue(string inputText)
194+
{
195+
await using var recorder = new TestRecorder(App);
196+
197+
var stackPanel = await LoadXaml<StackPanel>("""
198+
<StackPanel>
199+
<materialDesign:DecimalUpDown Minimum="-2.5" Maximum="2.5" Value="2.5" />
200+
<Button Content="AlternateFocusElement" />
201+
</StackPanel>
202+
""");
203+
var decimalUpDown = await stackPanel.GetElement<DecimalUpDown>();
204+
var textBox = await decimalUpDown.GetElement<TextBox>("PART_TextBox");
205+
206+
var button = await stackPanel.GetElement<Button>();
207+
208+
await textBox.MoveKeyboardFocus();
209+
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}{inputText}");
210+
await button.MoveKeyboardFocus();
211+
212+
Assert.Equal("2.5", await textBox.GetText());
213+
Assert.Equal(2.5m, await decimalUpDown.GetValue());
214+
215+
recorder.Success();
216+
}
150217
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ await Wait.For(async () =>
3333
Assert.Equal("1", await textBox.GetText());
3434
Assert.Equal(1, await numericUpDown.GetValue());
3535
});
36+
37+
recorder.Success();
3638
}
3739

3840
[Fact]
@@ -64,6 +66,8 @@ await Wait.For(async () =>
6466
});
6567

6668
Assert.True(await plusButton.GetIsEnabled());
69+
70+
recorder.Success();
6771
}
6872

6973
[Fact]
@@ -95,6 +99,8 @@ await Wait.For(async () =>
9599
});
96100

97101
Assert.True(await minusButton.GetIsEnabled());
102+
103+
recorder.Success();
98104
}
99105

100106
[Fact]
@@ -117,6 +123,8 @@ public async Task MaxAndMinAssignments_CoerceValueToBeInRange()
117123
Assert.Equal(3, await numericUpDown.GetValue());
118124
Assert.Equal(3, await numericUpDown.GetMinimum());
119125
Assert.Equal(3, await numericUpDown.GetMaximum());
126+
127+
recorder.Success();
120128
}
121129

122130
[Fact]

0 commit comments

Comments
 (0)