venerdì 13 settembre 2013

Force binding update for a TextBox

The TextBox control updates the Text binding source only when it looses focus.

When you are writing in a TextBox and you press a button in the ApplicationBar, the focus not changes and your MVVM property doesn't update.

The workaround is to force the binding update like this:
var focusedElement = FocusManager.GetFocusedElement();
var focusedTextBox = focusedElement as TextBox;

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

2 commenti:

  1. Hi,
    this solution is valid for WindowsPhone and Windows 8?

    1. Hi Daniele,
      it works only in WP. The "GetBindingExpression" is missing in Win8. You can use this workaround: