Skip to content

Instantly share code, notes, and snippets.

@taylorleese
Created July 8, 2010 17:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save taylorleese/468331 to your computer and use it in GitHub Desktop.
Save taylorleese/468331 to your computer and use it in GitHub Desktop.
WPF - Bindable PasswordBox
using System.Windows;
using System.Windows.Controls;
namespace CustomControl
{
public class BindablePasswordBox : Decorator
{
/// <summary>
/// The password dependency property.
/// </summary>
public static readonly DependencyProperty PasswordProperty;
private bool isPreventCallback;
private RoutedEventHandler savedCallback;
/// <summary>
/// Static constructor to initialize the dependency properties.
/// </summary>
static BindablePasswordBox()
{
PasswordProperty = DependencyProperty.Register(
"Password",
typeof(string),
typeof(BindablePasswordBox),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPasswordPropertyChanged))
);
}
/// <summary>
/// Saves the password changed callback and sets the child element to the password box.
/// </summary>
public BindablePasswordBox()
{
savedCallback = HandlePasswordChanged;
PasswordBox 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); }
}
/// <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)
{
BindablePasswordBox bindablePasswordBox = (BindablePasswordBox) d;
PasswordBox 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)
{
PasswordBox passwordBox = (PasswordBox) sender;
isPreventCallback = true;
Password = passwordBox.Password;
isPreventCallback = false;
}
}
}
@taylorleese
Copy link
Author

This is a workaround for the fact that the Password property of System.Windows.Controls.PasswordBox is not a DependencyProperty. This creates a new BindablePasswordBox using System.Windows.Controls.Decorator.

@Palatis
Copy link

Palatis commented Aug 17, 2013

How do I expose the "Padding" property of the underlying PasswordBox?

I tried to add a new DependencyProperty "PaddingProperty":

public static readonly DependencyProperty PaddingProperty = DependencyProperty.Register(
    "Padding", typeof(double), typeof(BindablePasswordBox),
    new FrameworkPropertyMetadata(new Thickness(2), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPaddingPropertyChanged))
);

public Thickness Padding
{
    get { return (Thickness)GetValue(PaddingProperty); }
    set { SetValue(PaddingProperty, value); }
}

but I cannot set it in XAML, says something about type mismatch.

Is there an attribute I can add to the Padding property so WPF will accept my values in xaml?

@dtaylor-530
Copy link

example xaml:

                <UniformGrid>
                    <Label VerticalAlignment="Center" HorizontalAlignment="Right">Password</Label>
                    <PasswordBox Height="20" Width="70" HorizontalAlignment="Left"></PasswordBox>
                </UniformGrid>

@msavazzi
Copy link

msavazzi commented Feb 7, 2021

@dtaylor-530 sorry to bother but something very weird happens to me. Ive added the code and the extension
<PasswordBox Margin="{StaticResource SmallTopMargin}" h:PasswordHelper.Attach="True" h:PasswordHelper.Password="{Binding BackupPassword, Mode=TwoWay}" mah:TextBoxHelper.UseFloatingWatermark="True" mah:TextBoxHelper.Watermark="{x:Static properties:Resources.SettingsPageBackupPwd}" Style="{StaticResource MahApps.Styles.PasswordBox.Button.Revealed}" ToolTip="{x:Static properties:Resources.SettingsPageBackupPwdTooltip}" />

I've put it in the settings page of a standard WPF app created with the visual studio template for Core 3.1
But when I leave the page I get an "empty" password:
BackupPassword >t< BackupPassword >te< BackupPassword >tes< BackupPassword >test< BackupPassword >< navigated from

to debug it I've added:
private string _backupPassword; public string BackupPassword { get { return _backupPassword; } set { Debug.WriteLine("BackupPassword >" + value+ "<"); Set(ref _backupPassword, value); } }

I'm using MVVM Light

@dtaylor-530
Copy link

@dtaylor-530 sorry to bother but something very weird happens to me. Ive added the code and the extension
<PasswordBox Margin="{StaticResource SmallTopMargin}" h:PasswordHelper.Attach="True" h:PasswordHelper.Password="{Binding BackupPassword, Mode=TwoWay}" mah:TextBoxHelper.UseFloatingWatermark="True" mah:TextBoxHelper.Watermark="{x:Static properties:Resources.SettingsPageBackupPwd}" Style="{StaticResource MahApps.Styles.PasswordBox.Button.Revealed}" ToolTip="{x:Static properties:Resources.SettingsPageBackupPwdTooltip}" />

I've put it in the settings page of a standard WPF app created with the visual studio template for Core 3.1
But when I leave the page I get an "empty" password:
BackupPassword >t< BackupPassword >te< BackupPassword >tes< BackupPassword >test< BackupPassword >< navigated from

to debug it I've added:
private string _backupPassword; public string BackupPassword { get { return _backupPassword; } set { Debug.WriteLine("BackupPassword >" + value+ "<"); Set(ref _backupPassword, value); } }

I'm using MVVM Light

@msavazzi hi
If your still having difficulties with this problem then i can take a look at what you have so far. I tried to clone your repository @https://github.com/msavazzi/Mahapps.PasswordBind but there were files missing including the UserControl with the PasswordBox.

@msavazzi
Copy link

msavazzi commented Feb 8, 2021

@dtaylor-530 still have the issue. Weird for the project as I just dumped the (clear) solution folder on git. Let me know if you need more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment