Skip to content

Commit

Permalink
fixed #422
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Jul 12, 2020
1 parent 8b48ce9 commit 7969435
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
Background="{DynamicResource RegionBrush}">
<hc:TransitioningContentControl>
<StackPanel Margin="32">
<hc:PasswordBox ShowClearButton="True" Password="123456"/>
<hc:PasswordBox Name="PasswordBoxDemo" ShowClearButton="True" Password="123456" IsSafeEnabled="False"/>
<TextBox Text="{Binding UnsafePassword,ElementName=PasswordBoxDemo,UpdateSourceTrigger=PropertyChanged}" Margin="0,6,0,0"/>
<hc:PasswordBox Password="123456" Margin="0,16,0,0"/>

<hc:PasswordBox ShowClearButton="True" hc:InfoElement.Title="{ex:Lang Key={x:Static langs:LangKeys.TitleDemoStr1}}" Password="123456" Margin="0,32,0,0"/>
Expand Down
44 changes: 42 additions & 2 deletions src/Shared/HandyControl_Shared/Controls/Input/PasswordBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class PasswordBox : Control, IDataInput

private System.Windows.Controls.TextBox _textBox;


/// <summary>
/// 掩码字符
/// </summary>
Expand Down Expand Up @@ -107,6 +106,40 @@ public bool ShowPassword
set => SetValue(ShowPasswordProperty, ValueBoxes.BooleanBox(value));
}

public static readonly DependencyProperty IsSafeEnabledProperty = DependencyProperty.Register(
"IsSafeEnabled", typeof(bool), typeof(PasswordBox), new PropertyMetadata(ValueBoxes.TrueBox, OnIsSafeEnabledChanged));

private static void OnIsSafeEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var p = (PasswordBox)d;
p.SetCurrentValue(UnsafePasswordProperty, !(bool)e.NewValue ? p.Password : string.Empty);
}

public bool IsSafeEnabled
{
get => (bool) GetValue(IsSafeEnabledProperty);
set => SetValue(IsSafeEnabledProperty, ValueBoxes.BooleanBox(value));
}

public static readonly DependencyProperty UnsafePasswordProperty = DependencyProperty.Register(
"UnsafePassword", typeof(string), typeof(PasswordBox), new FrameworkPropertyMetadata(default(string),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnUnsafePasswordChanged));

private static void OnUnsafePasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var p = (PasswordBox) d;
if (!p.IsSafeEnabled)
{
p.Password = e.NewValue != null ? e.NewValue.ToString() : string.Empty;
}
}

public string UnsafePassword
{
get => (string) GetValue(UnsafePasswordProperty);
set => SetValue(UnsafePasswordProperty, value);
}

public static readonly DependencyProperty MaxLengthProperty =
System.Windows.Controls.TextBox.MaxLengthProperty.AddOwner(typeof(PasswordBox));

Expand Down Expand Up @@ -193,6 +226,7 @@ public string Password
return;
}

if (Equals(ActualPasswordBox.Password, value)) return;
ActualPasswordBox.Password = value;
}
}
Expand Down Expand Up @@ -317,7 +351,13 @@ public void Clear()
_textBox.Clear();
}

private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e) => VerifyData();
private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (VerifyData() && !IsSafeEnabled)
{
SetCurrentValue(UnsafePasswordProperty, ActualPasswordBox.Password);
}
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) => VerifyData();
}
Expand Down

0 comments on commit 7969435

Please sign in to comment.