microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
UpdateSourceTrigger on PropertyChanged for NumberBox does not work
Describe the bug
Using x:Bind to create a two-way binding of data to the NumberBox.Value property and specifying UpdateSourceTrigger as PropertyChanged does not affect the behavior of the NumberBox. It appears that the behavior is remains to act as if UpdateSourceTrigger was set to LostFocus.
Steps to reproduce the bug
<NumberBox Header="Latitude (°)" PlaceholderText="0.0000000000" NumberFormatter="{x:Bind ViewModel.LatLonFormatter}" Value="{x:Bind ViewModel.Latitude, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Expected behavior
When the binding UpdateSourceTrigger is set to PropertyChanged, the behavior should be that any keystroke updates the ViewModel's backing property, but that does not happen until the NumberBox loses focus.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.0
Windows app type
- [ ] UWP
- [ ] Win32
Device form factor
Desktop
Windows version
Windows 11 (21H2): Build 22000
Additional context
No response
I'm having this problem also but I'm using Binding instead of x:Bind.
Are there any updates on this issue
Weird that this issue is still unresolved. The TextBox has the same behavior.
The problem seems to be that NumberBox doesn't update its Text and Value properties when the underlying TextBox gets input, instead only updating them when focus is lost. So UpdateSourceTrigger is doing what it's supposed to do, but the NumberBox itself isn't behaving how we'd expect since it doesn't update the Text/Value properties until focus loss.
Unfortunately this makes it difficult to use NumberBox in conjunction with keyboard events (for instance, submitting data when Enter is pressed). At minimum it would be nice to be able to programmatically tell NumberBox to update it's value from the entered text so we could work around this.
@garrettpauls Actually I managed to fixed it with a workaround.
the xaml:
<TextBox
x:Name="QueryTextBox"
PlaceholderText="{m:LanguageResource Key=SearchForProduct}"
Text="{Binding CommodityQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsTabStop="True"
Grid.Column="1"
Margin="2"
VerticalAlignment="Bottom"
KeyDown="QueryTextBox_KeyDown"/>
The code behind:
private void QueryTextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter && DataContext is RegisterViewModel viewModel && viewModel.QueryCommand.CanExecute(viewModel.CommodityQuery))
{
viewModel.QueryCommand.Execute(viewModel.CommodityQuery);
QueryTextBox.Focus(FocusState.Keyboard);
QueryTextBox.Text = string.Empty;
}
}
@IsmailHassani Unfortunately that won't work for NumberBox, as NumberBox currently doesn't support UpdateSourceTrigger=PropertyChanged for either Value or Text (TextBox does behave as expected as of this comment).
Short of forcing it to lose focus there doesn't seem to be a way to force NumberBox to update the parsed value. For now we've moved away from NumberBox and will implement custom key input handling for TextBox instead...
I've found this issue today as well and had to use a text box in which I validate the value manually as well. Would love to see this on numberbox. And as already mentioned it would be okay to force validation programmatically if necessary.
@IsmailHassani Unfortunately that won't work for NumberBox, as NumberBox currently doesn't support UpdateSourceTrigger=PropertyChanged for either Value or Text (TextBox does behave as expected as of this comment).
Short of forcing it to lose focus there doesn't seem to be a way to force NumberBox to update the parsed value. For now we've moved away from NumberBox and will implement custom key input handling for TextBox instead...
So is NumberBox
obsolete? Is it recommened to use TextBox
instead?