Files
IOT/LAEPS/Helpers/PasswordBoxHelper.cs
2025-12-23 10:55:34 +08:00

53 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace LAEPS.Helpers
{
public static class PasswordBoxHelper
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached(
"Password",
typeof(string),
typeof(PasswordBoxHelper),
new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnPasswordPropertyChanged));
public static void SetPassword(DependencyObject d, string value)
=> d.SetValue(PasswordProperty, value);
public static string GetPassword(DependencyObject d)
=> (string)d.GetValue(PasswordProperty);
private static void OnPasswordPropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
if (d is PasswordBox pb)
{
pb.PasswordChanged -= PasswordChanged;
if (pb.Password != (string)e.NewValue)
{
pb.Password = (string)e.NewValue;
}
pb.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
if (sender is PasswordBox pb)
{
SetPassword(pb, pb.Password);
}
}
}
}