Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UpdateSourceTrigger on PropertyChanged for NumberBox does not work #7188

Open
2 tasks
gwalschlager opened this issue Jun 5, 2022 · 8 comments
Open
2 tasks
Labels
area-NumberBox NumberBox Control bug Something isn't working product-winui3 WinUI 3 issues team-Controls Issue for the Controls team

Comments

@gwalschlager
Copy link

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

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 gwalschlager added the bug Something isn't working label Jun 5, 2022
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jun 5, 2022
@krschau krschau added team-Controls Issue for the Controls team product-winui3 WinUI 3 issues labels Jun 8, 2022
@ojhad ojhad added area-NumberBox NumberBox Control and removed needs-triage Issue needs to be triaged by the area owners labels Jun 9, 2022
@jhert0
Copy link

jhert0 commented Mar 10, 2023

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

@alraseensaad
Copy link

Are there any updates on this issue

@IsmailHassani
Copy link

IsmailHassani commented Oct 11, 2023

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

@garrettpauls
Copy link

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
Copy link

@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
Copy link

@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
Copy link

TRadigk commented Feb 18, 2024

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
Copy link

karmeye commented Jun 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NumberBox NumberBox Control bug Something isn't working product-winui3 WinUI 3 issues team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

9 participants