microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.29k stars 675 forks source link

UpdateSourceTrigger on PropertyChanged for NumberBox does not work #7188

Open gwalschlager opened 2 years ago

gwalschlager commented 2 years ago

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

Device form factor

Desktop

Windows version

Windows 11 (21H2): Build 22000

Additional context

No response

jhert0 commented 1 year ago

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

alraseensaad commented 11 months ago

Are there any updates on this issue

IsmailHassani commented 11 months ago

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

garrettpauls commented 11 months ago

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.

IsmailHassani commented 11 months ago

@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;
    }
}
garrettpauls commented 11 months ago

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

TRadigk commented 7 months ago

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.

karmeye commented 3 months ago

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

SoggyBottomBoy commented 1 week ago

Can anyone from the WinUI team comment on this? The problem with LostFocus for number box is that it is not always consistent (or doesn't appear to be in my scenario). I have form with multiple controls mostly TextBox, ComboBox, ToggleButton and NumberBox controls. The only problematic control is the Numberbox. For the TextBox I can set the UpdateSourceTrigger to PropertyChanged which updates immediately, however, when i set the Value on the number box to UpdateSourceTrigger it is not updated until focus is lost, depending on what the user clicks in the UI the value is not updated.

My scenario is: There is a list view with items The selected item is bound to a property in the view model I have a form control which is also bound to the selected item The user edits the NumberBox (but does not trigger a lost focus) The user clicks a different item in the list view, the number box never loses focus and the value remains in the number box which was never applied to the previously selected viewmodel. Now if the number box loses focus the change is applied the currently selected item.

SoggyBottomBoy commented 1 week ago

Also the CharacterReceived event is never triggered as far a I can tell.