首次提交
This commit is contained in:
71
WpfAppTest/PasswordBoxHelper.cs
Normal file
71
WpfAppTest/PasswordBoxHelper.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace WpfAppTest.Controls // 根据您的项目命名空间修改
|
||||
{
|
||||
public static class PasswordBoxHelper
|
||||
{
|
||||
public static readonly DependencyProperty HasPasswordProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"HasPassword",
|
||||
typeof(bool),
|
||||
typeof(PasswordBoxHelper),
|
||||
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
||||
|
||||
public static bool GetHasPassword(DependencyObject dp)
|
||||
{
|
||||
return (bool)dp.GetValue(HasPasswordProperty);
|
||||
}
|
||||
|
||||
public static void SetHasPassword(DependencyObject dp, bool value)
|
||||
{
|
||||
dp.SetValue(HasPasswordProperty, value);
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty MonitorPasswordProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"MonitorPassword",
|
||||
typeof(bool),
|
||||
typeof(PasswordBoxHelper),
|
||||
new UIPropertyMetadata(false, OnMonitorPasswordChanged));
|
||||
|
||||
public static bool GetMonitorPassword(DependencyObject dp)
|
||||
{
|
||||
return (bool)dp.GetValue(MonitorPasswordProperty);
|
||||
}
|
||||
|
||||
public static void SetMonitorPassword(DependencyObject dp, bool value)
|
||||
{
|
||||
dp.SetValue(MonitorPasswordProperty, value);
|
||||
}
|
||||
|
||||
private static void OnMonitorPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is PasswordBox passwordBox)
|
||||
{
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
|
||||
UpdateHasPassword(passwordBox); // Initial check
|
||||
}
|
||||
else
|
||||
{
|
||||
passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is PasswordBox passwordBox)
|
||||
{
|
||||
UpdateHasPassword(passwordBox);
|
||||
}
|
||||
}
|
||||
|
||||
private static void UpdateHasPassword(PasswordBox passwordBox)
|
||||
{
|
||||
SetHasPassword(passwordBox, passwordBox.Password.Length > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user