1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace WpfApplication1
{
public class PasswordBinder : Behavior<PasswordBox>
{
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(string), typeof(PasswordBinder), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPasswordChanged));
private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var binder = d as PasswordBinder;
if (binder == null)
return;
if (binder.AssociatedObject.Password != binder.Password)
binder.AssociatedObject.Password = binder.Password;
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PasswordChanged += PasswordBoxPasswordChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PasswordChanged -= PasswordBoxPasswordChanged;
}
void PasswordBoxPasswordChanged(object sender, RoutedEventArgs e)
{
this.Password = AssociatedObject.Password;
}
}
} |