0

Pretty much as the title says. It seems that access to the password contents is really only available in the code behind file, but how could this be passed into the view model?

This is just for a basic WPF login screen, and security is not of the upmost concern. However we would still prefer to use the PW box control.

thanks

Ammar Raja
  • 1,284
  • 5
  • 14
  • 23
user1089599
  • 303
  • 6
  • 20

2 Answers2

4

Usually I just pass the entire PasswordBox object to the LoginCommand via the CommandParameter

<Button Command="{Binding Path=LoginCommand}"
        CommandParameter="{Binding ElementName=MyPasswordBox}" ... />

Then I can cast the object as a PasswordBox, and get the value from PasswordBox.Password

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Could you please explain how the viewmodel would look for this example? Would the signature for LoginCommand have a single parameter of type PasswordBox? Thanks – user95227 Apr 10 '17 at 16:09
  • @user95227 Yes, that's correct. I know it breaks MVVM pattern for the purpose of login, but I'd rather do that then pass around the plain-text password. Another alternative might be to have a custom password box control that encrypts the text user enters, and have the VM compare that encrypted text with the stored encrypted text. – Rachel Apr 10 '17 at 18:02
-1

My xaml look like,

<PasswordBox Name="Password" Password="{Binding Pwd, Mode=TwoWay}"/>

in ViewModel

private string _Pwd;
public string Pwd
{
    get { return _Pwd; }
    set { 
        _Pwd = value;
        RaisePropertyChanged(()=>Pwd);
    }
}
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • 1
    'Binding' cannot be set on the 'Password' property of type 'PasswordBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. –  Nov 15 '12 at 10:49