60 lines
1.9 KiB
C#
60 lines
1.9 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 System.Windows.Controls;
|
|
|
|
namespace FATrace.WPLApp.Models
|
|
{
|
|
public class PasswordBoxBehavior : Behavior<PasswordBox>
|
|
{
|
|
public static readonly DependencyProperty PasswordProperty =
|
|
DependencyProperty.Register("Password", typeof(string), typeof(PasswordBoxBehavior),
|
|
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
|
OnPasswordPropertyChanged));
|
|
|
|
private bool _isUpdating;
|
|
|
|
public string Password
|
|
{
|
|
get { return (string)GetValue(PasswordProperty); }
|
|
set { SetValue(PasswordProperty, value); }
|
|
}
|
|
|
|
private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
{
|
|
var behavior = d as PasswordBoxBehavior;
|
|
if (behavior._isUpdating) return;
|
|
|
|
if (behavior.AssociatedObject != null)
|
|
{
|
|
behavior._isUpdating = true;
|
|
behavior.AssociatedObject.Password = e.NewValue?.ToString() ?? string.Empty;
|
|
behavior._isUpdating = false;
|
|
}
|
|
}
|
|
|
|
protected override void OnAttached()
|
|
{
|
|
base.OnAttached();
|
|
AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged;
|
|
}
|
|
|
|
protected override void OnDetaching()
|
|
{
|
|
AssociatedObject.PasswordChanged -= OnPasswordBoxValueChanged;
|
|
base.OnDetaching();
|
|
}
|
|
|
|
private void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e)
|
|
{
|
|
_isUpdating = true;
|
|
Password = AssociatedObject.Password;
|
|
_isUpdating = false;
|
|
}
|
|
}
|
|
}
|