Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Represents a way to bind to the password property while still being able to use the default PasswordBox
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
public class BindablePasswordBox : ContentControl
{
private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bindablePasswordBox = (BindablePasswordBox)d;
bindablePasswordBox.OnPasswordPropertyChanged((string)e.OldValue, (string)e.NewValue);
}
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(string), typeof(BindablePasswordBox)
, new FrameworkPropertyMetadata(string.Empty
, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal
, new PropertyChangedCallback(BindablePasswordBox.OnPasswordPropertyChanged)
, new CoerceValueCallback(/*CoerceText*/(d, v) => v == null ? (object)string.Empty : v)
, true
, UpdateSourceTrigger.LostFocus
)
);
public string Password
{
get { return (string)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}
private PasswordBox _passwordBox;
private RoutedEventHandler _passwordChanged;
private bool _isInsidePasswordContentChange;
public BindablePasswordBox()
{
Content = new PasswordBox();
}
protected override void OnContentChanged(object oldContent, object newContent)
{
Unsubscribe(oldContent);
base.OnContentChanged(oldContent, newContent);
Subscribe(newContent);
}
private void Subscribe(object newContent)
{
var passwordBox = newContent as PasswordBox;
if (passwordBox != null)
{
_passwordBox = passwordBox;
_passwordChanged = (sender, e) => {
_isInsidePasswordContentChange = true;
this.Password = passwordBox.Password;
_isInsidePasswordContentChange = false;
};
passwordBox.PasswordChanged += _passwordChanged; // subscribe event handler
}
}
private void Unsubscribe(object oldContent)
{
var passwordBox = oldContent as PasswordBox;
if (passwordBox != null)
{
passwordBox.PasswordChanged -= _passwordChanged; // unsubscribe event handler
}
}
private void OnPasswordPropertyChanged(string oldValue, string newValue)
{
if (_isInsidePasswordContentChange)
{
return;
}
var passwordBox = _passwordBox;
if (passwordBox != null)
{
passwordBox.PasswordChanged -= _passwordChanged; // unsubscribe event handler
passwordBox.Password = newValue;
passwordBox.PasswordChanged += _passwordChanged; // resubscribe event handler
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment