How to bind to a PasswordBox in MVVM

前端 未结 30 1776
执念已碎
执念已碎 2020-11-22 11:50

I have come across a problem with binding to a PasswordBox. It seems it\'s a security risk but I am using the MVVM pattern so I wish to bypass this. I found som

30条回答
  •  心在旅途
    2020-11-22 12:37

    I used this method and passed the password box, although this does violate the MVVM it was essential for me because I was using a content control with data template for my login within my shell which is a complex shell enviroment. So accessing the code behind of the shell would have been crap.

    Passing the passwordbox I would think is same as accessing control from code behind as far as I know. I agree passwords, dont keep in memory etc In this implementation I don't have property for password in view model.

    Button Command

    Command="{Binding Path=DataContext.LoginCommand, ElementName=MyShell}" CommandParameter="{Binding ElementName=PasswordBox}"
    

    ViewModel

    private void Login(object parameter)
    {
        System.Windows.Controls.PasswordBox p = (System.Windows.Controls.PasswordBox)parameter;
        MessageBox.Show(p.Password);
    }
    

提交回复
热议问题