"UpdateSourceTrigger = PropertyChanged" equivalent for Windows Phone 7 TextBox

Is there a way to get a TextBox in Windows Phone 7 to update the binding as the user enters each letter, and not after losing focus?

As in the following WPF TextBox:

<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/> 
+34
c # windows-phone-7 silverlight
Jan 28 2018-11-21T00:
source share
8 answers

Silverlight for WP7 does not support the syntax you specified. Instead, do the following:

 <TextBox TextChanged="OnTextBoxTextChanged" Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=Explicit}" /> 

UpdateSourceTrigger = Explicit is a great bonus here. What it is? Explicit : Updates the binding source only when the UpdateSource method is UpdateSource . It saves one additional set of bindings when the user leaves the TextBox .

In C #:

 private void OnTextBoxTextChanged( object sender, TextChangedEventArgs e ) { TextBox textBox = sender as TextBox; // Update the binding source BindingExpression bindingExpr = textBox.GetBindingExpression( TextBox.TextProperty ); bindingExpr.UpdateSource(); } 
+51
Jan 28 2018-11-21T00:
source share
— -

I like to use an attached property. Just in case, you get into these little buggers.

 <toolkit:DataField Label="Name"> <TextBox Text="{Binding Product.Name, Mode=TwoWay}" c:BindingUtility.UpdateSourceOnChange="True"/> </toolkit:DataField> 

And then the support code.

 public class BindingUtility { public static bool GetUpdateSourceOnChange(DependencyObject d) { return (bool)d.GetValue(UpdateSourceOnChangeProperty); } public static void SetUpdateSourceOnChange(DependencyObject d, bool value) { d.SetValue(UpdateSourceOnChangeProperty, value); } // Using a DependencyProperty as the backing store for … public static readonly DependencyProperty UpdateSourceOnChangeProperty = DependencyProperty.RegisterAttached( "UpdateSourceOnChange", typeof(bool), typeof(BindingUtility), new PropertyMetadata(false, OnPropertyChanged)); private static void OnPropertyChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) { var textBox = d as TextBox; if (textBox == null) return; if ((bool)e.NewValue) { textBox.TextChanged += OnTextChanged; } else { textBox.TextChanged -= OnTextChanged; } } static void OnTextChanged(object s, TextChangedEventArgs e) { var textBox = s as TextBox; if (textBox == null) return; var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty); if (bindingExpression != null) { bindingExpression.UpdateSource(); } } } 
+23
Jan 29 '11 at 2:18
source share

Not through the binding syntax, no, but it's easy enough. You must handle the TextChanged event and call UpdateSource to bind.

 private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { ((TextBox) sender).GetBindingExpression( TextBox.TextProperty ).UpdateSource(); } 

This can easily be converted to sticky behavior .

+5
Jan 28 2018-11-21T00:
source share

In the event call to TextChanged UpdateSource () .

 BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty); be.UpdateSource(); 
+1
Jan 28 2018-11-21T00:
source share

You can write your own TextBox behavior to handle Update on TextChanged:

This is my example in PasswordBox, but you can just change it to handle any property of any object.

 public class UpdateSourceOnPasswordChangedBehavior : Behavior<PasswordBox> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.PasswordChanged += OnPasswordChanged; } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PasswordChanged -= OnPasswordChanged; } private void OnPasswordChanged(object sender, RoutedEventArgs e) { AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource(); } } 

Ussage:

 <PasswordBox x:Name="Password" Password="{Binding Password, Mode=TwoWay}" > <i:Interaction.Behaviors> <common:UpdateSourceOnPasswordChangedBehavior/> </i:Interaction.Behaviors> </PasswordBox> 
+1
Aug 23 '12 at 20:00
source share

UpdateSourceTrigger = Explicit does not work for me, so Im using a native class derived from TextBox

 public class TextBoxEx : TextBox { public TextBoxEx() { TextChanged += (sender, args) => { var bindingExpression = GetBindingExpression(TextProperty); if (bindingExpression != null) { bindingExpression.UpdateSource(); } }; } } 
0
Jun 25 2018-12-12T00:
source share

This is just one line of code!

 (sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource(); 

You can create a general TextChanged event (for example, "ImmediateTextBox_TextChanged") in the code behind your page and associate it with any text field on the page.

0
Jun 29 '12 at 11:33
source share

I accepted the Praetorian answer and made an extension class that inherits a TextBox , so you don't need to confuse your view code with this behavior.

C-Sharp :

 public class TextBoxUpdate : TextBox { public TextBoxUpdate() { TextChanged += OnTextBoxTextChanged; } private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e) { TextBox senderText = (TextBox)sender; BindingExpression bindingExp = senderText.GetBindingExpression(TextBox.TextProperty); bindingExp.UpdateSource(); } } 

Visualbasic

 Public Class TextBoxUpdate : Inherits TextBox Private Sub OnTextBoxTextChanged(sender As Object, e As TextChangedEventArgs) Handles Me.TextChanged Dim senderText As TextBox = DirectCast(sender, TextBox) Dim bindingExp As BindingExpression = senderText.GetBindingExpression(TextBox.TextProperty) bindingExp.UpdateSource() End Sub End Class 

Then call this in XAML :

 <local:TextBoxUpdate Text="{Binding PersonName, Mode=TwoWay}"/> 
0
Dec 02 '13 at 2:08 on
source share



All Articles