Skip to content

Commit

Permalink
Fix PasswordBox issues when PasswordRevealMode is Visible
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Sep 7, 2020
1 parent c02bba7 commit 1cc1d53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
56 changes: 28 additions & 28 deletions ModernWpf/Controls/Primitives/PasswordBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void SetPasswordRevealMode(PasswordBox passwordBox, PasswordReveal
private static void OnPasswordRevealModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var helper = GetHelperInstance((PasswordBox)d);
helper?.UpdateVisualState();
helper?.UpdateVisualState(true);
}

#endregion
Expand Down Expand Up @@ -204,7 +204,7 @@ private void Detach()
{
TextBox.CommandBindings.Remove(TextBoxCutBinding);
TextBox.CommandBindings.Remove(TextBoxCopyBinding);
TextBox.TextChanged -= OnTextChanged;
TextBox.TextChanged -= OnTextBoxTextChanged;
TextBox = null;
}
}
Expand All @@ -219,38 +219,43 @@ private void OnApplyTemplate()
{
_passwordBox.ApplyTemplate();

var template = _passwordBox.Template;
if (template != null)
{
TextBox = GetTemplateChild(nameof(TextBox)) as TextBox;

if (TextBox != null)
{
TextBox.IsUndoEnabled = false;
SpellCheck.SetIsEnabled(TextBox, false);
TextBox.CommandBindings.Add(TextBoxCutBinding);
TextBox.CommandBindings.Add(TextBoxCopyBinding);
TextBox.TextChanged += OnTextChanged;
}
TextBox = _passwordBox.GetTemplateChild<TextBox>(nameof(TextBox));

if (TextBox != null)
{
TextBox.IsUndoEnabled = false;
SpellCheck.SetIsEnabled(TextBox, false);
TextBox.CommandBindings.Add(TextBoxCutBinding);
TextBox.CommandBindings.Add(TextBoxCopyBinding);
TextBox.TextChanged += OnTextBoxTextChanged;
UpdateTextBox();
UpdateVisualState();
}

UpdateVisualState(false);
}

private void OnGotFocus(object sender, RoutedEventArgs e)
{
if (PasswordRevealMode == PasswordRevealMode.Visible && TextBox != null)
{
if (e.OriginalSource == _passwordBox)
{
TextBox.Focus();
e.Handled = true;
}
}

if (!string.IsNullOrEmpty(_passwordBox.Password))
{
_hideRevealButton = true;
}

UpdateVisualState();
UpdateVisualState(true);
}

private void OnLostFocus(object sender, RoutedEventArgs e)
{
UpdateVisualState();
UpdateVisualState(true);
}

private void OnPasswordChanged(object sender, RoutedEventArgs e)
Expand All @@ -264,10 +269,10 @@ private void OnPasswordChanged(object sender, RoutedEventArgs e)

SetPlaceholderTextVisibility(_passwordBox, hasPassword ? Visibility.Collapsed : Visibility.Visible);
UpdateTextBox();
UpdateVisualState();
UpdateVisualState(true);
}

private void OnTextChanged(object sender, TextChangedEventArgs e)
private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (PasswordRevealMode == PasswordRevealMode.Visible)
{
Expand All @@ -277,13 +282,13 @@ private void OnTextChanged(object sender, TextChangedEventArgs e)

private void UpdateTextBox()
{
if (TextBox != null && PasswordRevealMode != PasswordRevealMode.Visible)
if (TextBox != null)
{
TextBox.Text = _passwordBox.Password;
}
}

private void UpdateVisualState()
private void UpdateVisualState(bool useTransitions)
{
bool buttonVisible = false;
if (_passwordBox.IsFocused)
Expand All @@ -300,12 +305,7 @@ private void UpdateVisualState()
}
}

VisualStateManager.GoToState(_passwordBox, buttonVisible ? ButtonVisibleState : ButtonCollapsedState, true);
}

private DependencyObject GetTemplateChild(string childName)
{
return _passwordBox.Template?.FindName(childName, _passwordBox) as DependencyObject;
VisualStateManager.GoToState(_passwordBox, buttonVisible ? ButtonVisibleState : ButtonCollapsedState, useTransitions);
}
}
}
1 change: 1 addition & 0 deletions ModernWpf/Styles/PasswordBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
ContextMenu="{TemplateBinding ContextMenu}"
local:TextContextMenu.UsingTextContextMenu="{TemplateBinding local:TextContextMenu.UsingTextContextMenu}"
Visibility="Collapsed"
IsTabStop="False"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
<TextBlock
Expand Down

0 comments on commit 1cc1d53

Please sign in to comment.