Files
YuPu-OrpaonEMS/OrpaonEMS.App/PasswordBoxHelper.cs
2025-02-28 22:23:13 +08:00

71 lines
2.3 KiB
C#

using MaterialDesignThemes.Wpf;
using OrpaonEMS.App;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static OrpaonEMS.App.CANDrive.CAN;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using System.Windows;
namespace OrpaonEMS.App
{
public static class PasswordBoxHelper
{
public static readonly DependencyProperty BoundPasswordProperty =
DependencyProperty.RegisterAttached("BoundPassword",
typeof(string), typeof(PasswordBoxHelper),
new PropertyMetadata(string.Empty, OnBoundPasswordChanged));
public static string GetBoundPassword(DependencyObject d)
{
return (string)d.GetValue(BoundPasswordProperty);
}
public static void SetBoundPassword(DependencyObject d, string value)
{
d.SetValue(BoundPasswordProperty, value);
}
private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is PasswordBox passwordBox)
{
passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
if (!GetUpdatingPassword(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
}
}
private static readonly DependencyProperty UpdatingPasswordProperty =
DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false));
private static bool GetUpdatingPassword(DependencyObject d)
{
return (bool)d.GetValue(UpdatingPasswordProperty);
}
private static void SetUpdatingPassword(DependencyObject d, bool value)
{
d.SetValue(UpdatingPasswordProperty, value);
}
private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (sender is PasswordBox passwordBox)
{
SetUpdatingPassword(passwordBox, true);
SetBoundPassword(passwordBox, passwordBox.Password);
SetUpdatingPassword(passwordBox, false);
}
}
}
}