wpfui
wpfui copied to clipboard
NumberBox not updating displayed value after changing value
NumberBox does not update the displayed value (Text property) after changing Value property if the control is not empty.
To Reproduce
Steps to reproduce the behavior:
- Create NumberBox
<wpfui:NumberBox x:Name="TEST" MinWidth="100" Step="1" Min="1" Max="100" />
- Create button with click event
<Button x:Name="Button" Content="Change Value" Click="Button_Click" />
private void Button_Click(object sender, RoutedEventArgs e) {
TEST.Value = 20;
}
- Run application and set NumberBox to 13 (or any other number)
- Click button, notice that NumberBox still displays original number
Expected behavior
I expect when changing Value for the new number to be reflected in the NumberBox
Desktop (please complete the following information):
- OS: Windows 10
- .NET: net6.0
- Version: WPFUI 1.2.7
Additional context
This appears to be an issue in Value_PropertyChanged
: https://github.com/lepoco/wpfui/blob/8883c3a2a1125b75801066626e6f4d9f137dfd88/src/WPFUI/Controls/NumberBox.cs#L189
If the string is not null or empty the code returns. I’d be happy to submit a pull request but not sure what the intent of this line is.
In the mean time the simple workaround is to set Text
and Value
properties.
Thanks for the great library!
The same happen with binding. The displayed value for TextBox is updating while NumberBox is not.
<ui:NumberBox Value="{Binding Test}" />
<ui:TextBox Text="{Binding Test}" />
<Button Command="{Binding TestIncrement}"></Button>
It updates when using .Text property:
NumberBox.Text = "3";
Clear button doesn't have an Event, it would be nice to have it so it doesn't return to the min value, but a custom one
I'm guessing the Value
is changed after the Text
property. Thus if you're using the TextChanged
event, it updates the text as expected - but Value
has not yet been updated.
This seems like a pretty big oversight since there is no ValueChanged
event. There is however Incremented
and Decremented
, but having to use two events when one would suffice is a bit meh.
Here's my temporary (non-binding) solution for the problem, if you need a binding you can probably make a converter that does something similar;
private void NumberBox_TextChanged(object sender, CultureInfo.InvariantCulture, TextChangedEventArgs e)
{
var numBox = ((NumberBox)sender);
double newValue;
if (double.TryParse(numBox.Text, out newValue))
numBox.Value = newValue;
}
This updates the Value
property whenever the text is changed, works just fine and dandy for my needs.
Edit:
Forgot to include CultureInfo.InvariantCulture
, otherwise parsing fails due to formatting error.
This seems to be fixed in v3.0.0
I just tested v3.0.2 and doesn't seem to be fixed.
Here's my reproduction: https://github.com/tmijail/WPFUINumberBoxBugDemo/
At least when changing the NumberBox
's value in the codebehind, the issue seems fixed.
https://github.com/lepoco/wpfui/assets/6920322/8e44890c-5f06-4a10-88ba-18ad570d60bf
When using bindings (see ViewModel branch), it doesn't work some of the time, but thats because of issue #945
https://github.com/lepoco/wpfui/assets/6920322/eedc6d82-e90a-4e46-9f84-9b975756299d