@@ -81,8 +81,67 @@ public static Brush GetPlaceholderForegroundBrush(UIElement element)
8181 return ( Brush ) element . GetValue ( PlaceholderForegroundBrushProperty ) ;
8282 }
8383
84- }
8584
8685
8786
87+ #region PasswordBox Attached Prperties
88+
89+ internal static bool GetIsPasswordBoxExtensionAttached ( DependencyObject obj )
90+ {
91+ return ( bool ) obj . GetValue ( IsPasswordBoxExtensionAttachedProperty ) ;
92+ }
93+ internal static void SetIsPasswordBoxExtensionAttached ( DependencyObject obj , bool value )
94+ {
95+ obj . SetValue ( IsPasswordBoxExtensionAttachedProperty , value ) ;
96+ }
97+ // Using a DependencyProperty as the backing store for IsPasswordBoxExtensionAttached. This enables animation, styling, binding, etc...
98+ public static readonly DependencyProperty IsPasswordBoxExtensionAttachedProperty =
99+ DependencyProperty . RegisterAttached ( "IsPasswordBoxExtensionAttached" , typeof ( bool ) , typeof ( Extensions ) , new PropertyMetadata ( false , OnIsPasswordBoxExtensionAttachedChanged ) ) ;
100+
101+ private static void OnIsPasswordBoxExtensionAttachedChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
102+ {
103+ var ctrl = d as PasswordBox ;
104+ var newValue = ( bool ) e . NewValue ;
105+ var oldValue = ( bool ) e . OldValue ;
106+ if ( ctrl == null ) return ;
107+
108+ // 無効になった場合の処理
109+ if ( oldValue && ! newValue )
110+ {
111+ ctrl . PasswordChanged -= Ctrl_PasswordChanged ;
112+ }
113+
114+ // 有効になった場合の処理
115+ if ( ! oldValue && newValue )
116+ {
117+ ctrl . PasswordChanged += Ctrl_PasswordChanged ;
118+ var value = string . IsNullOrEmpty ( ctrl . Password ) ;
119+ SetIsPasswordEmpty ( ctrl , value ) ;
120+ }
121+ }
122+
123+ private static void Ctrl_PasswordChanged ( object sender , RoutedEventArgs e )
124+ {
125+ var ctrl = sender as PasswordBox ;
126+ if ( ctrl != null )
127+ {
128+ var value = string . IsNullOrEmpty ( ctrl . Password ) ;
129+ SetIsPasswordEmpty ( ctrl , value ) ;
130+ }
131+ }
132+
133+ public static bool GetIsPasswordEmpty ( DependencyObject obj )
134+ {
135+ return ( bool ) obj . GetValue ( IsPasswordEmptyProperty ) ;
136+ }
137+ private static void SetIsPasswordEmpty ( DependencyObject obj , bool value )
138+ {
139+ obj . SetValue ( IsPasswordEmptyProperty , value ) ;
140+ }
141+ // Using a DependencyProperty as the backing store for IsPasswordEmpty. This enables animation, styling, binding, etc...
142+ public static readonly DependencyProperty IsPasswordEmptyProperty =
143+ DependencyProperty . RegisterAttached ( "IsPasswordEmpty" , typeof ( bool ) , typeof ( Extensions ) , new PropertyMetadata ( true ) ) ;
144+
145+ #endregion
146+ }
88147}
0 commit comments