Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- #endregion
- namespace Cmc.Installer.Core.Controls
- {
- public sealed class BindablePasswordBox : Decorator
- {
- /// <summary>
- /// The password dependency property.
- /// </summary>
- public static readonly DependencyProperty PasswordProperty;
- public static readonly DependencyProperty BorderBrushProperty;
- private readonly RoutedEventHandler _savedCallback;
- private bool _isPreventCallback;
- /// <summary>
- /// Static constructor to initialize the dependency properties.
- /// </summary>
- static BindablePasswordBox()
- {
- PasswordProperty = DependencyProperty.Register(
- "Password",
- typeof (string),
- typeof (BindablePasswordBox),
- new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
- OnPasswordPropertyChanged)
- );
- BorderBrushProperty = DependencyProperty.Register(
- "BorderBrush",
- typeof(Brush),
- typeof(BindablePasswordBox),
- new PropertyMetadata(default(Brush))
- );
- }
- /// <summary>
- /// Saves the password changed callback and sets the child element to the password box.
- /// </summary>
- public BindablePasswordBox()
- {
- _savedCallback = HandlePasswordChanged;
- var passwordBox = new PasswordBox();
- passwordBox.PasswordChanged += _savedCallback;
- Child = passwordBox;
- }
- /// <summary>
- /// The password dependency property.
- /// </summary>
- public string Password
- {
- get { return GetValue(PasswordProperty) as string; }
- set { SetValue(PasswordProperty, value); }
- }
- public Brush BorderBrush
- {
- get { return (Brush)GetValue(BorderBrushProperty); }
- set { SetValue(BorderBrushProperty, value); }
- }
- /// <summary>
- /// Handles changes to the password dependency property.
- /// </summary>
- /// <param name="d">the dependency object</param>
- /// <param name="eventArgs">the event args</param>
- private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs)
- {
- var bindablePasswordBox = (BindablePasswordBox) d;
- var passwordBox = (PasswordBox) bindablePasswordBox.Child;
- if (bindablePasswordBox._isPreventCallback)
- {
- return;
- }
- passwordBox.PasswordChanged -= bindablePasswordBox._savedCallback;
- passwordBox.Password = (eventArgs.NewValue != null) ? eventArgs.NewValue.ToString() : "";
- passwordBox.PasswordChanged += bindablePasswordBox._savedCallback;
- }
- /// <summary>
- /// Handles the password changed event.
- /// </summary>
- /// <param name="sender">the sender</param>
- /// <param name="eventArgs">the event args</param>
- private void HandlePasswordChanged(object sender, RoutedEventArgs eventArgs)
- {
- var passwordBox = (PasswordBox) sender;
- _isPreventCallback = true;
- Password = passwordBox.Password;
- _isPreventCallback = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement