microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

UpdateSourceTrigger on PropertyChanged for NumberBox does not work

Open gwalschlager opened this issue 2 years ago • 7 comments

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

gwalschlager avatar Jun 05 '22 08:06 gwalschlager

I'm having this problem also but I'm using Binding instead of x:Bind.

jhert0 avatar Mar 10 '23 17:03 jhert0

Are there any updates on this issue

alraseensaad avatar Sep 29 '23 23:09 alraseensaad

Weird that this issue is still unresolved. The TextBox has the same behavior.

IsmailHassani avatar Oct 11 '23 18:10 IsmailHassani

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 avatar Oct 13 '23 21:10 garrettpauls

@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 avatar Oct 13 '23 21:10 IsmailHassani

@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...

garrettpauls avatar Oct 16 '23 14:10 garrettpauls

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.

TRadigk avatar Feb 18 '24 13:02 TRadigk

@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?

karmeye avatar Jun 03 '24 05:06 karmeye