当尝试数据绑定一个PasswordBox的密码属性,你会认识到,你不能做数据就可以了绑定。 这样做的原因是,该密码属性不是由DependencyProperty支持。
原因是数据绑定密码是不是一个好的设计,出于安全考虑,应该避免。 但有时这种安全是没有必要的,那么它不但麻烦,你不能绑定到Password属性。 在这种特殊情况下,你可以利用以下PasswordBoxBindingHelper的。
<PasswordBox Name="pwdBoxPWD" tools:PasswordBoxBindingHelper.IsPasswordBindingEnabled="True" tools:PasswordBoxBindingHelper.BindedPassword= "{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="38" Margin="167,170,0,0" PasswordChar="*" VerticalAlignment="Top" Width="258" FontSize="24" VerticalContentAlignment="Center" MaxLength="18" />
PasswordBoxBindingHelper通过调用PasswordBoxBindingHelper.BindedPasswordProperty附着到passwordbox。附加属性PasswordBoxBindingHelper.Password提供了PasswordBox控制的原始密码属性的拷贝PassWord用于绑定。
1 public static class PasswordBoxBindingHelper 2 { 3 //BindedPassword 4 public static readonly DependencyProperty BindedPasswordProperty = 5 DependencyProperty.RegisterAttached("BindedPassword", 6 typeof(string), typeof(PasswordBoxBindingHelper), 7 new FrameworkPropertyMetadata(string.Empty, OnBindedPasswordChanged)); 8 9 public static string GetBindedPassword(DependencyObject dp) 10 { 11 return (string)dp.GetValue(BindedPasswordProperty); 12 } 13 public static void SetBindedPassword(DependencyObject dp, string value) 14 { 15 dp.SetValue(BindedPasswordProperty, value); 16 } 17 18 private static void OnBindedPasswordChanged(DependencyObject sender, 19 DependencyPropertyChangedEventArgs e) 20 { 21 PasswordBox passwordBox = sender as PasswordBox; 22 if (passwordBox == null) 23 { 24 return; 25 } 26 passwordBox.PasswordChanged -= PasswordChanged; 27 28 if (!(bool)GetIsUpdating(passwordBox)) 29 { 30 passwordBox.Password = (string)e.NewValue; 31 } 32 passwordBox.PasswordChanged += PasswordChanged; 33 } 34 35 36 //IsPasswordBindingEnabled 37 public static readonly DependencyProperty IsPasswordBindingEnabledProperty = 38 DependencyProperty.RegisterAttached("IsPasswordBindingEnabled", 39 typeof(bool), typeof(PasswordBoxBindingHelper), new PropertyMetadata(false, IsPasswordBindingEnabled)); 40 public static void SetIsPasswordBindingEnabled(DependencyObject dp, bool value) 41 { 42 dp.SetValue(IsPasswordBindingEnabledProperty, value); 43 } 44 45 public static bool GetIsPasswordBindingEnabled(DependencyObject dp) 46 { 47 return (bool)dp.GetValue(IsPasswordBindingEnabledProperty); 48 } 49 private static void IsPasswordBindingEnabled(DependencyObject sender, 50 DependencyPropertyChangedEventArgs e) 51 { 52 PasswordBox passwordBox = sender as PasswordBox; 53 54 if (passwordBox == null) 55 return; 56 57 if ((bool)e.OldValue) 58 { 59 passwordBox.PasswordChanged -= PasswordChanged; 60 } 61 62 if ((bool)e.NewValue) 63 { 64 passwordBox.PasswordChanged += PasswordChanged; 65 } 66 } 67 68 //IsUpdating 69 private static readonly DependencyProperty IsUpdatingProperty = 70 DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), 71 typeof(PasswordBoxBindingHelper)); 72 73 private static bool GetIsUpdating(DependencyObject dp) 74 { 75 return (bool)dp.GetValue(IsUpdatingProperty); 76 } 77 78 private static void SetIsUpdating(DependencyObject dp, bool value) 79 { 80 dp.SetValue(IsUpdatingProperty, value); 81 } 82 83 private static void PasswordChanged(object sender, RoutedEventArgs e) 84 { 85 PasswordBox passwordBox = sender as PasswordBox; 86 SetIsUpdating(passwordBox, true); 87 SetBindedPassword(passwordBox, passwordBox.Password); 88 SetIsUpdating(passwordBox, false); 89 } 90 } 91 }