2025-08-15 15:25:44 +08:00

52 lines
1.5 KiB
C#

using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Telerik.Windows.Controls;
namespace DI_Electrical.Helper
{
public class PasswordBoxBehavior : Behavior<RadPasswordBox>
{
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register(nameof(Password), typeof(string), typeof(PasswordBoxBehavior),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPasswordChanged));
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PasswordChanged += OnPasswordChanged;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PasswordChanged -= OnPasswordChanged;
}
private void OnPasswordChanged(object sender, RoutedEventArgs e)
{
Password = AssociatedObject.Password;
}
private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is PasswordBoxBehavior behavior)
{
behavior.AssociatedObject.Password = e.NewValue as string ?? string.Empty;
}
}
}
}