0

View:


TextBox x:Name="feedback" Text="{Binding FeedbackText,Mode=TwoWay}"

ViewModel:

public string FeedbackText
{
get
{
        return _feedbackTextProperty;
}

set
{
        _feedbackTextProperty = value;
    RaisePropertyChanged(FeedbackTextPropertyName);
}
}

I am using a bindable application bar but when I click the button there is no value in the FeedbackText property. It looks as if "lostfocus" is not firing to update the property.

I am using MVVM Light. Have I missed something?

Declan
  • 499
  • 4
  • 10
  • I haven't worked with MVVM Light, but I'm wondering if your missing an implementation (such as the INotifyPropertyChanged interface). – MattN May 16 '12 at 12:07
  • Possible duplicate of http://stackoverflow.com/questions/5569768/textbox-binding-twoway-doesnt-update-until-focus-lost-wp7 – Kevin Gosse May 16 '12 at 12:22

2 Answers2

0

If you still had focus in the textbox when you clicked the app bar button the textbox won't fire the lost focus event and cause teh binding to update.

Yes, this can be frustrating. :(

There are various work arounds such as forcibly updating the binding in such a situation or the Binding Helper in the Coding4Fun Tools.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143
  • Thanks Matt, Can you explain how to "forcibly update" the binding. I am not using Coding4Fun Tools and don't wish to add another DLL just for this at the moment. – Declan May 16 '12 at 13:10
  • The simple solution seems to be to use a standard button rather than an applicationbar button. – Declan May 16 '12 at 13:22
  • try `BindingOperations.GetBindingExpressionBase(dependencyObject, dependencyProperty).UpdateTarget();` – Matt Lacey May 16 '12 at 15:26
0

I hope that I am not too late. I had the same problem using Window Phone 8 saving the TextBox text when pressing an ApplicationBarIconButton. A way to fix this issue is to update the binding source property of the focused TextBox. You can do that with the following code:

var focusedObject = FocusManager.GetFocusedElement() as TextBox;

if (focusedObject != null)
{
    var binding = focusedObject.GetBindingExpression(TextBox.TextProperty);

    if (binding != null)
    {
        binding.UpdateSource();
    }
}

Best!

Ozzy Garcia
  • 656
  • 5
  • 8