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();
}
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>
- 3 回答
- 0 关注
- 425 浏览
添加回答
举报