Windowscommunitytoolkit: MVVM Toolkit - Binding to TextBox.Text

Created on 26 Nov 2020  路  9Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

Describe the bug

When binding the Text property on a TextBox to a property on a view model, the view model property doesn't get set until the TextBox is unfocused.

  • [ ] Is this bug a regression in the toolkit? (No)

Steps to Reproduce

  • [ ] Can this be reproduced in the Sample App? (Either in a sample as-is or with new XAML pasted in the editor.) If so, please provide custom XAML or steps to reproduce. If not, let us know why it can't be reproduced (e.g. more complex setup, environment, dependencies, etc...)

Steps to reproduce the behavior:

  1. Create a blank UWP project
  2. Install Microsoft.Toolkit.Mvvm preview 4
  3. Follow the Putting things together guide to create a view model with a string property and use it in MainPage
  4. Add the following to MainPage.xaml:
<TextBox Text="{x:Bind ViewModel.TheStringProperty, Mode=TwoWay}"/>
  1. Set a breakpoint on the setter for ViewModel.TheStringProperty
  2. Observe that the breakpoint is not hit until after the TextBox is unfocused

Expected behavior

If I were to bind something to ViewModel.TheStringProperty, it should behave exactly the same as if I had bound directly to the TextBox's Text property. (That is, ViewModel.TheStringProperty should be set as soon as the text changes.)

Screenshots

If applicable, add screenshots to help explain your problem.

Environment


NuGet Package(s):
Microsoft.Toolkit.Mvvm

Package Version(s):
Preview 4

Windows 10 Build Number:

  • [ ] Fall Creators Update (16299)
  • [ ] April 2018 Update (17134)
  • [ ] October 2018 Update (17763)
  • [ ] May 2019 Update (18362)
  • [x] May 2020 Update (19041)
  • [ ] Insider Build (build number: )

App min and target version:

  • [ ] Fall Creators Update (16299)
  • [ ] April 2018 Update (17134)
  • [x] October 2018 Update (17763)
  • [ ] May 2019 Update (18362)
  • [x] May 2020 Update (19041)
  • [ ] Insider Build (xxxxx)

Device form factor:

  • [x] Desktop
  • [ ] Xbox
  • [ ] Surface Hub
  • [ ] IoT

Visual Studio

  • [ ] 2017 (version: )
  • [x] 2019 (version: 16.8.2)
  • [ ] 2019 Preview (version: )
not an issue question

Most helpful comment

This is the by design behavior through for a TextBox. Referring the Doc: https://docs.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth#binding-object-declared-using-binding

I dont think this is a bug for XAML

All 9 comments

Hello yoshiask, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 馃檶

@yoshiask this is the default behavior of XAML unrelated to the toolkit. See the note in the docs:

By default, changes to TextBox.Text are sent to a two-way bound source when the TextBox loses focus. To cause changes to be sent after every user keystroke, set UpdateSourceTrigger to PropertyChanged on the binding in markup.

@Sergio0694 is there an example of this in the sample app for the MVVM Toolkit?

@michael-hawker Yup, we're using UpdateSourceTrigger=PropertyChanged in the interactive sample for the very first SetProperty example, with the binding to a TextBox 馃檪

@yoshiask That's in the ObservableObject sample page, should help as a reference and to see that in action!

@yoshiask you good with us closing this issue?

The reason I opened the issue is because this behavior is actually different than what I get when binding directly to the property in code behind. Maybe this is a bug with XAML then?

@yoshiask can you expand on what you mean by "binding directly to the property in code behind"? Are you talking about creating a Binding object manually or something else?

This is the expected behavior though for a TextBox, as per the docs.

This is the by design behavior through for a TextBox. Referring the Doc: https://docs.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth#binding-object-declared-using-binding

I dont think this is a bug for XAML

@yoshiask you good with us closing this issue?

Going to close the issue as this is the default behavior.

Was this page helpful?
0 / 5 - 0 ratings