71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace ShrlAlgo.Toolkit.Wpf.Attach
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
} |