Two-way data binding from TextBox is not updated when a button is clicked in ApplicationBar

I have a TextBox in my application and ApplicationBarIconButton in the ApplicationBar , which acts as a β€œsubmit” for the contents of the TextBox .

When editing a TextBox using the virtual keyboard, ApplicationBarIconButton is still displayed under SIP, so you can immediately send text without firing the keyboard: nice!

However, when the button is clicked, the view model to which the TextBox is bound does not update.

I found someone else with the same problem here , and they used a rather nasty workaround for manually updating the viewmodel on the TextBox TextChanged event.

Removes all the elegance of using HTML data view models!

Is this a bug in WP7?

Or is there a better way that I haven't found yet?

+4
source share
3 answers

The problem is that Silverlight bindings do not support PropertyChanged for UpdateSourceTrigger. This means that, by default, a TextBox will update the property attached to the Text when the TextBox loses focus, and the only other way is to explicitly update it in the code, as was done in the example from your link.

You have only two options: update the binding when you click the button or remove focus from the text field when you click the button.

I usually update the binding in the TextChanged event. To do this, I use the extension method:

 public static void UpdateBinding(this TextBox textBox) { BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty); if (bindingExpression != null) { bindingExpression.UpdateSource(); } } 

lets me just call it code:

 textBox.UpdateBinding(); 

You can also use custom behavior for this.

+13
source

A brief description of the steps that allow you to attach work to each click of a text field, and not just when the text field loses focus. Uses a prism. This is a slightly indirect solution to the original problem.

  • In the NuGet Package Manager, search for Prism. Add Prism.Phone, created by Microsoft Samples and Techniques.

  • Add the following to your phone page: PhoneApplicationPage tag

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:prismInteractivity="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"

  • Give your text block a separate closing tag and add the following between opening and closing TextBox tags

<i:Interaction.Behaviors> <prismInteractivity:UpdateTextBindingOnPropertyChanged/> </i:Interaction.Behaviors>

+1
source

I think this will work, but you should check if this is really necessary for the ApplicationBarIconButton (or just the buttons on the page).

Often you should avoid this when you like having a good Metro design in your application, which you might prefer to use InputScope = "Search" + SIP hiding is easily done using Page.Focus ()

eg. (old article, InputScope = "Search" worked for me) http://4mkmobile.com/2011/02/wp7-devs-stop-adding-search-buttons/

See also: http://forums.create.msdn.com/forums/p/70506/619517.aspx#619517

private void SearchTextBox_KeyUp (object handler, KeyEventArgs e):

  • Using InputScope = "Search" for my search field
  • Using DataBinding = TwoWay Mode
  • Focus (); // hides SIP
  • UpdateBinding (SearchTextBox); // trick mentioned here
  • App.ViewModel.ExecuteSearch ();

Works great in my MVVM application.

0
source

Source: https://habr.com/ru/post/1381715/


All Articles