This commit is contained in:
NaBian committed 2020-07-12 21:26:06 +08:00
1 parent 8b48ce958d
commit 79694355de
2 files changed
+44 -3

No files matched your search

@@ -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"/>
@@ -26,7 +26,6 @@ namespace HandyControl.Controls
private System.Windows.Controls.TextBox _textBox;
/// <summary>
/// 掩码字符
/// </summary>
@@ -107,6 +106,40 @@ namespace HandyControl.Controls
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));
@@ -193,6 +226,7 @@ namespace HandyControl.Controls
return;
}
if (Equals(ActualPasswordBox.Password, value)) return;
ActualPasswordBox.Password = value;
}
}
@@ -317,7 +351,13 @@ namespace HandyControl.Controls
_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();
}