lunedì 24 giugno 2013

[WPF] PasswordBox e DataBinding

Nativamente la passwordbox non consente si effettuare il databinding perché la proprità Password non è una di tipo DependencyProperty. Per ovviare a ciò non ci resta che creare la classe di supporto PasswordHelper:

public static class PasswordHelper
    {
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.RegisterAttached("Password",
            typeof(string), typeof(PasswordHelper),
            new FrameworkPropertyMetadata(string.EmptyOnPasswordPropertyChanged));
 
        public static readonly DependencyProperty AttachProperty =
            DependencyProperty.RegisterAttached("Attach",
            typeof(bool), typeof(PasswordHelper), new PropertyMetadata(falseAttach));
 
        private static readonly DependencyProperty IsUpdatingProperty =
           DependencyProperty.RegisterAttached("IsUpdating"typeof(bool),
           typeof(PasswordHelper));
 
 
        public static void SetAttach(DependencyObject dpbool value)
        {
            dp.SetValue(AttachPropertyvalue);
        }
 
        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }
 
        public static string GetPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(PasswordProperty);
        }
 
        public static void SetPassword(DependencyObject dpstring value)
        {
            dp.SetValue(PasswordPropertyvalue);
        }
 
        private static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }
 
        private static void SetIsUpdating(DependencyObject dpbool value)
        {
            dp.SetValue(IsUpdatingPropertyvalue);
        }
 
        private static void OnPasswordPropertyChanged(DependencyObject sender,
            DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            passwordBox.PasswordChanged -= PasswordChanged;
 
            if (!(bool)GetIsUpdating(passwordBox))
            {
                passwordBox.Password = (string)e.NewValue;
            }
            passwordBox.PasswordChanged += PasswordChanged;
        }
 
        private static void Attach(DependencyObject sender,
            DependencyPropertyChangedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
 
            if (passwordBox == null)
                return;
 
            if ((bool)e.OldValue)
            {
                passwordBox.PasswordChanged -= PasswordChanged;
            }
 
            if ((bool)e.NewValue)
            {
                passwordBox.PasswordChanged += PasswordChanged;
            }
        }
 
        private static void PasswordChanged(object senderRoutedEventArgs e)
        {
            PasswordBox passwordBox = sender as PasswordBox;
            SetIsUpdating(passwordBoxtrue);
            SetPassword(passwordBoxpasswordBox.Password);
            SetIsUpdating(passwordBoxfalse);
        }
    }

XAML:
xmlns:helper="clr-namespace:XXX.Utility" 
<PasswordBox helper:PasswordHelper.Attach="True" helper:PasswordHelper.Password="{Binding Password,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />

Nessun commento:

Posta un commento