为了账号安全,请及时绑定邮箱和手机立即绑定

Windows Phone 7文本框的“ UpdateSourceTrigger

Windows Phone 7文本框的“ UpdateSourceTrigger

C#
UYOU 2019-12-26 14:22:44
有没有一种方法可以让Windows Phone 7中的TextBox在用户键入每个字母而不是失去焦点后更新绑定?就像下面的WPF TextBox一样:<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>
查看完整描述

3 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

WP7的Silverlight不支持您列出的语法。而是执行以下操作:


<TextBox TextChanged="OnTextBoxTextChanged"

         Text="{Binding MyText, Mode=TwoWay,

                UpdateSourceTrigger=Explicit}" />

UpdateSourceTrigger = Explicit在这里是明智的选择。它是什么? 显式:仅在调用UpdateSource方法时更新绑定源。当用户离开时,它为您节省了一个额外的绑定集TextBox。


在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();

}


查看完整回答
反对 回复 2019-12-26
?
慕桂英3389331

TA贡献2036条经验 获得超8个赞

您可以编写自己的TextBox Behavior以处理TextChanged上的Update:


这是我对PasswordBox的示例,但是您可以简单地对其进行更改以处理任何对象的任何属性。


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();

    }

}

用法:


<PasswordBox x:Name="Password" Password="{Binding Password, Mode=TwoWay}" >

    <i:Interaction.Behaviors>

        <common:UpdateSourceOnPasswordChangedBehavior/>

    </i:Interaction.Behaviors>

</PasswordBox>


查看完整回答
反对 回复 2019-12-26
  • 3 回答
  • 0 关注
  • 406 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信