Skip to content

Commit ceeb1b7

Browse files
committed
fix floating hint issue on returning to a screen fixes #193
2 parents 4b48296 + ac694e1 commit ceeb1b7

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

MaterialDesignThemes.Wpf/PasswordFieldAssist.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ private static void ManagedPropertyChangedCallback(DependencyObject dependencyOb
1717
if (passwordBox != null)
1818
{
1919
passwordBox.PasswordChanged -= PasswordBoxOnPasswordChanged;
20+
passwordBox.Loaded -= PasswordBoxOnPasswordChanged;
2021
}
2122

2223
passwordBox = dependencyPropertyChangedEventArgs.NewValue as PasswordBox;
2324
if (passwordBox != null)
2425
{
2526
passwordBox.PasswordChanged += PasswordBoxOnPasswordChanged;
26-
// call listener immediately, maybe passwordbox was not empty
27-
PasswordBoxOnPasswordChanged(passwordBox, new RoutedEventArgs());
27+
passwordBox.Loaded += PasswordBoxOnPasswordChanged;
2828
}
2929
}
3030

@@ -57,34 +57,57 @@ private static void ConfigureHint(PasswordBox passwordBox)
5757
passwordBox.SetValue(HintVisibilityProperty, passwordBox.SecurePassword.Length == 0 ? Visibility.Visible : Visibility.Hidden);
5858
}
5959

60+
/// <summary>
61+
/// Framework use only.
62+
/// </summary>
6063
public static readonly DependencyProperty HintVisibilityProperty = DependencyProperty.RegisterAttached(
6164
"HintVisibility", typeof(Visibility), typeof(PasswordFieldAssist), new PropertyMetadata(default(Visibility)));
6265

66+
/// <summary>
67+
/// Framework use only.
68+
/// </summary>
6369
public static void SetHintVisibility(DependencyObject element, Visibility value)
6470
{
6571
element.SetValue(HintVisibilityProperty, value);
6672
}
6773

74+
/// <summary>
75+
/// Framework use only.
76+
/// </summary>
6877
public static Visibility GetHintVisibility(DependencyObject element)
6978
{
7079
return (Visibility)element.GetValue(HintVisibilityProperty);
7180
}
7281

82+
/// <summary>
83+
/// Framework use only.
84+
/// </summary>
85+
/// <param name="element"></param>
86+
/// <param name="value"></param>
7387
public static void SetManaged(DependencyObject element, PasswordBox value)
7488
{
7589
element.SetValue(ManagedProperty, value);
7690
}
7791

92+
/// <summary>
93+
/// Framework use only.
94+
/// </summary>
7895
public static PasswordBox GetManaged(DependencyObject element)
7996
{
8097
return (PasswordBox)element.GetValue(ManagedProperty);
8198
}
8299

100+
/// <summary>
101+
/// Framework use only.
102+
/// </summary>
83103
private static void SetIsNullOrEmpty(DependencyObject element, bool value)
84104
{
85105
element.SetValue(IsNullOrEmptyPropertyKey, value);
86106
}
87107

108+
/// <summary>
109+
/// Framework use only.
110+
/// </summary>
88111
public static bool GetIsNullOrEmpty(DependencyObject element)
89112
{
90113
return (bool)element.GetValue(IsNullOrEmptyProperty);

MaterialDesignThemes.Wpf/TextFieldAssist.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.TextBox.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113

114114
<Style x:Key="MaterialDesignFloatingHintTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource MaterialDesignTextBox}">
115115
<Setter Property="wpf:TextFieldAssist.Text" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
116+
<Setter Property="wpf:TextFieldAssist.Managed" Value="{Binding RelativeSource={RelativeSource Self}}" />
116117
<Setter Property="Template">
117118
<Setter.Value>
118119
<ControlTemplate TargetType="TextBox">

0 commit comments

Comments
 (0)