Windows Phone 7文本框的“ UpdateSourceTrigger

有没有一种方法可以让Windows Phone 7中的TextBox在用户键入每个字母而不是失去焦点后更新绑定?


就像下面的WPF TextBox一样:


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


UYOU
浏览 408回答 3
3回答

慕后森

WP7的Silverlight不支持您列出的语法。而是执行以下操作:<TextBox TextChanged="OnTextBoxTextChanged"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text="{Binding MyText, Mode=TwoWay,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateSourceTrigger=Explicit}" />UpdateSourceTrigger = Explicit在这里是明智的选择。它是什么? 显式:仅在调用UpdateSource方法时更新绑定源。当用户离开时,它为您节省了一个额外的绑定集TextBox。在C#中:private void OnTextBoxTextChanged( object sender, TextChangedEventArgs e ){&nbsp; TextBox textBox = sender as TextBox;&nbsp; // Update the binding source&nbsp; BindingExpression bindingExpr = textBox.GetBindingExpression( TextBox.TextProperty );&nbsp; bindingExpr.UpdateSource();}

慕桂英3389331

您可以编写自己的TextBox Behavior以处理TextChanged上的Update:这是我对PasswordBox的示例,但是您可以简单地对其进行更改以处理任何对象的任何属性。public class UpdateSourceOnPasswordChangedBehavior&nbsp; &nbsp; &nbsp;: Behavior<PasswordBox>{&nbsp; &nbsp; protected override void OnAttached()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnAttached();&nbsp; &nbsp; &nbsp; &nbsp; AssociatedObject.PasswordChanged += OnPasswordChanged;&nbsp; &nbsp; }&nbsp; &nbsp; protected override void OnDetaching()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnDetaching();&nbsp; &nbsp; &nbsp; &nbsp; AssociatedObject.PasswordChanged -= OnPasswordChanged;&nbsp; &nbsp; }&nbsp; &nbsp; private void OnPasswordChanged(object sender, RoutedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty).UpdateSource();&nbsp; &nbsp; }}用法:<PasswordBox x:Name="Password" Password="{Binding Password, Mode=TwoWay}" >&nbsp; &nbsp; <i:Interaction.Behaviors>&nbsp; &nbsp; &nbsp; &nbsp; <common:UpdateSourceOnPasswordChangedBehavior/>&nbsp; &nbsp; </i:Interaction.Behaviors></PasswordBox>
打开App,查看更多内容
随时随地看视频慕课网APP