TextBox Binding TwoWay is not updated until focus loses WP7

I have a page with some text fields for data entry. To bind a text field, TwoWay . The data in my view model is updated only if the text field loses focus. If I click a button, such as saving, and the text field still has focus, the changes in the text field do not change in my view model in the save event.

Is there any way to bind to keep the value of the text field before it loses focus? Or do I need to do something in a save event?

+42
data-binding windows-phone-7 xaml
Apr 6 2018-11-11T00:
source share
8 answers

You can use the UpdateTextBindingOnPropertyChanged behavior from the Prism Library for WP7 to update the associated value when the text changes instead of the lost focus.

+7
Apr 6 2018-11-11T00:
source share

I assume that your Save button is an ApplicationBarButton (not a regular button). For regular buttons, this will just work because they are focused, and therefore data binding will begin.

For ApplicationBarButtons on the phone, this is slightly different, because they do not distract attention from the client application. To provide forced data binding when you click the "Save" button, you can add the following code to your handler:

 object focusObj = FocusManager.GetFocusedElement(); if (focusObj != null && focusObj is TextBox) { var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty); binding.UpdateSource(); } 
+55
Apr 6 2018-11-21T00:
source share

Download the free book by Charles Petzold Windows Phone 7 Programming . On page 387, he talks about how to do this.

Basically, set the UpdateSourceTrigger property of Binding to Explicit . Then, in the TextBox TextChanged update the binding source.

+16
Apr 6 2018-11-11T00:
source share

I am going in the opposite direction @Praetorian.

Your TextBox has the UpdateSourceTrigger value UpdateSourceTrigger to UpdateSourceTrigger by default. This means that the value is only pushed into your ViewModel property when it loses focus.

You can set UpdateSourceTrigger to PropertyChanged:

 <TextBox UpdateSourceTrigger="PropertyChanged" Text="{Binding TextViewModelProperty}" /> 

From http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx :

One of the values โ€‹โ€‹of UpdateSourceTrigger. The default value is the default value that returns the value of the UpdateSourceTrigger default property of the target. However, the default value for most dependency properties is PropertyChanged, and the text property has a default value of LostFocus.

Remember, this means that everything that is an update trigger for this property will happen much more often (basically with every keystroke, instead of a single "flash" when the TextBox loses focus).

Hope this helps!

+6
Apr 6 '11 at 18:19
source share

Here is a quick access answer to the Microsoft solution proposed by Derek. Instead of loading and sifting all the Prism items, simply copy this class into your project, and then follow these steps to activate it:

UpdateBindingOnPropertyChangedBehviour.cs

 using System; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Interactivity; namespace MyCompany.MyProduct { /// <summary> /// Custom behavior that updates the source of a binding on a text box as the text changes. /// </summary> public class UpdateTextBindingOnPropertyChanged : Behavior<TextBox> { /// <summary> /// Binding expression this behavior is attached to. /// </summary> private BindingExpression _expression; /// <summary> /// Called after the behavior is attached to an AssociatedObject. /// </summary> /// <remarks> /// Override this to hook up functionality to the AssociatedObject. /// </remarks> protected override void OnAttached() { base.OnAttached(); // Hook events to change behavior _expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty); AssociatedObject.TextChanged += OnTextChanged; } /// <summary> /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred. /// </summary> /// <remarks> /// Override this to unhook functionality from the AssociatedObject. /// </remarks> protected override void OnDetaching() { base.OnDetaching(); // Un-hook events AssociatedObject.TextChanged -= OnTextChanged; _expression = null; } /// <summary> /// Updates the source property when the text is changed. /// </summary> private void OnTextChanged(object sender, EventArgs args) { _expression.UpdateSource(); } } } 

This is basically a cleaned-up version of Microsoft Prism 4.1 code (see the Silverlight \ Prism.Interactivity project if you want to see the rest).

Now how to use it:

  • Add a link to the System.Windows.Interactivity assembly to your Windows Phone project.
  • On each page where you want to use the behavior, add a XAML link to the assembly: XMLNS: i = "CLR names: System.Windows.Interactivity; assembly = System.Windows.Interactivity"
  • Inside the XAML of each TextBox that you want to apply bahvior to (which already has a TwoWay binding to your original property), add the following:

    <i: Interaction.Behaviors>
    <my: UpdateTextBindingOnPropertyChanged / ">
    </ I: Interaction.Behaviors>

    Note: the prefix "my:" may differ in your code. This is just a namespace reference in which you added a behavior class.

+6
02 Oct '12 at 18:54
source share

try setting the UpdateSourceTrigger property to PropertyChanged

like this

 Property="{Binding PropertyBinding, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
+4
Aug 01 '16 at 12:56 on
source share

I have not tried the @Praetorian answer, so if this works well then do it - otherwise use the KeyUp and TextChanged events to update the binding source.

+1
Apr 6 2018-11-11T00:
source share

This link has a solution that works great in WinRT. It inherits a TextBox and adds a new property: BindableText.

http://www.familie-smits.com/post/2012/07/29/UpdateSourceTrigger-in-WinRT.aspx

0
Apr 16 '13 at 1:12
source share



All Articles