@@ -52,6 +52,12 @@ public static class TextFieldAssist
5252 private static readonly DependencyProperty IsNullOrEmptyProperty =
5353 IsNullOrEmptyPropertyKey . DependencyProperty ;
5454
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+
5561 #endregion
5662
5763 #region Public Methods and Operators
@@ -132,6 +138,24 @@ public static string GetText(DependencyObject element)
132138 return ( string ) element . GetValue ( TextProperty ) ;
133139 }
134140
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+
135159 #endregion
136160
137161 #region Methods
@@ -199,11 +223,44 @@ private static void TextPropertyChangedCallback(DependencyObject dependencyObjec
199223 else
200224 {
201225 frameworkElement . Loaded += ( sender , args ) => VisualStateManager . GoToState ( frameworkElement , state , false ) ;
226+ frameworkElement . IsVisibleChanged += ( sender , args ) => VisualStateManager . GoToState ( frameworkElement , state , true ) ;
202227 }
203228
204229 SetIsNullOrEmpty ( dependencyObject , string . IsNullOrEmpty ( ( dependencyPropertyChangedEventArgs . NewValue ?? "" ) . ToString ( ) ) ) ;
205230 }
206231
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+
207264 private static void SetIsNullOrEmpty ( DependencyObject element , bool value )
208265 {
209266 element . SetValue ( IsNullOrEmptyPropertyKey , value ) ;
0 commit comments