“UpdateSourceTrigger=PropertyChanged” equivalent f

2019-01-03 13:32发布

Is there a way to get a TextBox in Windows Phone 7 to update the Binding as the user types each letter rather than after losing focus?

Like the following WPF TextBox would do:

<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>

8条回答
贼婆χ
2楼-- · 2019-01-03 14:12

In TextChanged event call UpdateSource().

BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
查看更多
看我几分像从前
3楼-- · 2019-01-03 14:13

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

This is my sample to PasswordBox but you can simple change it to handle any property of the 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>
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-03 14:13

It's just one line of code!

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

You can create a generic TextChanged event (for example "ImmediateTextBox_TextChanged") in the code behind of your page, and than link it to any TextBox in the page.

查看更多
Ridiculous、
5楼-- · 2019-01-03 14:21

Silverlight for WP7 does not support the syntax you've listed. Do the following instead:

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

UpdateSourceTrigger = Explicit is a smart bonus here. What is it? Explicit: Updates the binding source only when you call the UpdateSource method. It saves you one extra binding set 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();
}
查看更多
我命由我不由天
6楼-- · 2019-01-03 14:21

UpdateSourceTrigger=Explicit doesnt work for me, hence Im using custom class derivated from TextBox

public class TextBoxEx : TextBox
{
    public TextBoxEx()
    {
        TextChanged += (sender, args) =>
                           {
                               var bindingExpression = GetBindingExpression(TextProperty);
                               if (bindingExpression != null)
                               {
                                   bindingExpression.UpdateSource();
                               }
                           };
    }

}
查看更多
冷血范
7楼-- · 2019-01-03 14:24

I took Praetorian's answer and made an extension class that inherits TextBox so you don't have to muddle up your view's code behind 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 like this in XAML:

<local:TextBoxUpdate Text="{Binding PersonName, Mode=TwoWay}"/>
查看更多
登录 后发表回答