20230201_145_upperpc/InSituLaboratory/Base/PasswordEx.cs
2024-03-11 13:12:02 +08:00

48 lines
1.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
namespace InSituLaboratory.Base
{
public class PasswordEx
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached(
"Password",
typeof(string),
typeof(PasswordEx),
new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
public static string GetPassword(DependencyObject d)
{
return (string)d.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject d, string value)
{
d.SetValue(PasswordProperty, value);
}
static bool _isUpdating = false;
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
PasswordBox pb = (d as PasswordBox);
pb.PasswordChanged -= Pb_PasswordChanged;
if (!_isUpdating)
pb.Password = e.NewValue.ToString();
pb.PasswordChanged += Pb_PasswordChanged;
}
private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox pb = (sender as PasswordBox);
_isUpdating = true;
SetPassword(pb, pb.Password);
_isUpdating = false;
}
}
}