0

My PasswordBox is bound to the ViewModel like:

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
.
.
.
<PasswordBox x:Name="pwbox" >
    <i:Interaction.Triggers>
         <i:EventTrigger EventName="PasswordChanged" >
               <i:InvokeCommandAction Command="{Binding PasswordChangedCommand }"  CommandParameter="{Binding ElementName=pwbox,  Mode=OneWay}"/>
         </i:EventTrigger>
    </i:Interaction.Triggers>
 </PasswordBox>

And in the view model:

public ICommand PasswordChangedCommand { get; private set; }
private string password;
public MyVMClass()
{
  PasswordChangedCommand = new RelayCommand<object>(PasswordChangedMethod);
}
private void PasswordChangedMethod(object obj)
{           
    password = ((System.Windows.Controls.PasswordBox)obj).Password;
}

It works fine in one way, i.e If I enter the password inside view, I can access it from the viewmodel, my question: how can I bound the password to the other way, i.e if I change it via ViewModel I want to see the change in the view.

BugsFixer
  • 377
  • 2
  • 15

1 Answers1

1

Wrong way. PasswordBox component is an exception.

If you going to make it two-way bind-able you broke security concept, because it's mean that somewhere in RAM password will be stored as PLAIN text, in your case in variable

private string password

Until last moment (real username/password validation), always try to pass password as ((PasswordBox)o).SecurePassword. Yes, it's broke MVVM pattern, but this is cost of security.

Zam
  • 2,880
  • 1
  • 18
  • 33