@@ -52,6 +52,12 @@ public static class TextFieldAssist
52
52
private static readonly DependencyProperty IsNullOrEmptyProperty =
53
53
IsNullOrEmptyPropertyKey . DependencyProperty ;
54
54
55
+ /// <summary>
56
+ /// Framework use only.
57
+ /// </summary>
58
+ public static readonly DependencyProperty ManagedProperty = DependencyProperty . RegisterAttached (
59
+ "Managed" , typeof ( TextBox ) , typeof ( TextFieldAssist ) , new PropertyMetadata ( default ( TextBox ) , ManagedPropertyChangedCallback ) ) ;
60
+
55
61
#endregion
56
62
57
63
#region Public Methods and Operators
@@ -132,6 +138,24 @@ public static string GetText(DependencyObject element)
132
138
return ( string ) element . GetValue ( TextProperty ) ;
133
139
}
134
140
141
+ /// <summary>
142
+ /// Framework use only.
143
+ /// </summary>
144
+ public static void SetManaged ( DependencyObject element , TextBox value )
145
+ {
146
+ element . SetValue ( ManagedProperty , value ) ;
147
+ }
148
+
149
+ /// <summary>
150
+ /// Framework use only.
151
+ /// </summary>
152
+ /// <param name="element"></param>
153
+ /// <returns></returns>
154
+ public static TextBox GetManaged ( DependencyObject element )
155
+ {
156
+ return ( TextBox ) element . GetValue ( ManagedProperty ) ;
157
+ }
158
+
135
159
#endregion
136
160
137
161
#region Methods
@@ -199,11 +223,44 @@ private static void TextPropertyChangedCallback(DependencyObject dependencyObjec
199
223
else
200
224
{
201
225
frameworkElement . Loaded += ( sender , args ) => VisualStateManager . GoToState ( frameworkElement , state , false ) ;
226
+ frameworkElement . IsVisibleChanged += ( sender , args ) => VisualStateManager . GoToState ( frameworkElement , state , true ) ;
202
227
}
203
228
204
229
SetIsNullOrEmpty ( dependencyObject , string . IsNullOrEmpty ( ( dependencyPropertyChangedEventArgs . NewValue ?? "" ) . ToString ( ) ) ) ;
205
230
}
206
231
232
+ private static void ManagedPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
233
+ {
234
+ var textBox = dependencyPropertyChangedEventArgs . OldValue as TextBox ;
235
+ if ( textBox != null )
236
+ {
237
+ textBox . IsVisibleChanged -= ManagedTextBoxOnIsVisibleChanged ;
238
+ }
239
+
240
+ textBox = dependencyPropertyChangedEventArgs . NewValue as TextBox ;
241
+ if ( textBox != null )
242
+ {
243
+ textBox . IsVisibleChanged += ManagedTextBoxOnIsVisibleChanged ;
244
+ }
245
+ }
246
+
247
+ private static void ManagedTextBoxOnIsVisibleChanged ( object sender , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
248
+ {
249
+ var textBox = ( TextBox ) sender ;
250
+
251
+ if ( ! textBox . IsVisible ) return ;
252
+
253
+ var state = string . IsNullOrEmpty ( textBox . Text )
254
+ ? "MaterialDesignStateTextEmpty"
255
+ : "MaterialDesignStateTextNotEmpty" ;
256
+
257
+ //yep, had to invoke post this to trigger refresh
258
+ textBox . Dispatcher . BeginInvoke ( new Action ( ( ) =>
259
+ {
260
+ VisualStateManager . GoToState ( textBox , state , false ) ;
261
+ } ) ) ;
262
+ }
263
+
207
264
private static void SetIsNullOrEmpty ( DependencyObject element , bool value )
208
265
{
209
266
element . SetValue ( IsNullOrEmptyPropertyKey , value ) ;
0 commit comments