有没有办法让 Windows Phone 7 中的 TextBox 在用户键入每个字母时而不是在失去焦点后更新绑定(bind)?
就像下面的 WPF TextBox 一样:
<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>
最佳答案
Silverlight for WP7 不支持您列出的语法。请改为执行以下操作:
<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 theUpdateSource
method. It saves you one extra binding set when the user leaves theTextBox
.
在 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();
}
关于c# - "UpdateSourceTrigger=PropertyChanged"相当于 Windows Phone 7 文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833100/