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 { 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; } } } }