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 關注
- 446 瀏覽
添加回答
舉報