using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace SamplePreSystem.UI.BaseControls { public class PasswordAttaExt { public static string GetCustomPwd(DependencyObject obj) { return (string)obj.GetValue(CustomPwdProperty); } public static void SetCustomPwd(DependencyObject obj, string value) { obj.SetValue(CustomPwdProperty, value); } // Using a DependencyProperty as the backing store for CustomPwd. This enables animation, styling, binding, etc... public static readonly DependencyProperty CustomPwdProperty = DependencyProperty.RegisterAttached("CustomPwd", typeof(string), typeof(PasswordAttaExt), new PropertyMetadata("", OnPropertyChangeBack)); private static void OnPropertyChangeBack(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox pb = d as PasswordBox; if (pb != null) { if (e.NewValue.ToString() != pb.Password) { pb.Password = e.NewValue.ToString(); } } } #region 设置事件绑定触发条件 public static bool GetIsOpenPwdBading(DependencyObject obj) { return (bool)obj.GetValue(IsOpenPwdBadingProperty); } public static void SetIsOpenPwdBading(DependencyObject obj, bool value) { obj.SetValue(IsOpenPwdBadingProperty, value); } // Using a DependencyProperty as the backing store for IsOpenPwdBading. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsOpenPwdBadingProperty = DependencyProperty.RegisterAttached("IsOpenPwdBading", typeof(bool), typeof(PasswordAttaExt), new PropertyMetadata(false, OnValEventChangeBack)); private static void OnValEventChangeBack(DependencyObject d, DependencyPropertyChangedEventArgs e) { PasswordBox pd = d as PasswordBox; if (pd != null) { pd.PasswordChanged -= Pd_PasswordChanged; pd.PasswordChanged += Pd_PasswordChanged; } } private static void Pd_PasswordChanged(object sender, RoutedEventArgs e) { PasswordBox pb = sender as PasswordBox; if (pb != null) { SetCustomPwd((DependencyObject)sender, pb.Password); } } #endregion } }