Silverlight4-Behavior-即时更新绑定的Password行为。
一些平常用到的行为,慢慢整理下。从最简单的开始。
TextBox默认的更新行为是在失去焦点时进行的,为了方便在输入框的值发生变化时就即时更新,在Prism里就提供了即时更新绑定的Text的行为,叫做“UpdateTextBindingOnPropertyChanged”。
同样的事也发生在PasswordBox里,有时候在绑定Password的时候也需要即时更新输入的绑定值,那么参考这个行为随便写个。
整个代码也非常简单,如下:
1 public class UpdatePasswordBindingOnPropertyChanged : Behavior<PasswordBox>
2 {
3 private BindingExpression _expression;
4
5 protected override void OnAttached()
6 {
7 base.OnAttached();
8
9 _expression = this.AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty);
10 this.AssociatedObject.PasswordChanged += this.OnPasswordChanged;
11 }
12
13 protected override void OnDetaching()
14 {
15 base.OnDetaching();
16
17 this.AssociatedObject.PasswordChanged -= this.OnPasswordChanged;
18 _expression = null;
19 }
20
21 private void OnPasswordChanged(object sender, EventArgs args)
22 {
23 _expression.UpdateSource();
24 }
25 }
2 {
3 private BindingExpression _expression;
4
5 protected override void OnAttached()
6 {
7 base.OnAttached();
8
9 _expression = this.AssociatedObject.GetBindingExpression(PasswordBox.PasswordProperty);
10 this.AssociatedObject.PasswordChanged += this.OnPasswordChanged;
11 }
12
13 protected override void OnDetaching()
14 {
15 base.OnDetaching();
16
17 this.AssociatedObject.PasswordChanged -= this.OnPasswordChanged;
18 _expression = null;
19 }
20
21 private void OnPasswordChanged(object sender, EventArgs args)
22 {
23 _expression.UpdateSource();
24 }
25 }
半路和尚 by 超时空饭盒 is licensed under a Creative Commons 署名-非商业性使用-相同方式共享 3.0 Unported License.
基于halfwaymonk.cnblogs.com上的作品创作。