How to bind to a PasswordBox in MVVM

前端 未结 30 1742
执念已碎
执念已碎 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:20

    Here's my take on it:

    1. Using an attached property to bind the password defeats the purpose of securing the password. The Password property of a password box is not bindable for a reason.

    2. Passing the password box as command parameter will make the ViewModel aware of the control. This will not work if you plan to make your ViewModel reusable cross platform. Don't make your VM aware of your View or any other controls.

    3. I don't think introducing a new property, an interface, subscribing to password changed events or any other complicated things is necessary for a simple task of providing the password.

    XAML

    
    

    Code behind - using code behind does not necessarily violate MVVM. As long as you don't put any business logic in it.

    btnLogin.CommandParameter = new Func(()=>pbPassword.Password); 
    

    ViewModel

    LoginCommand = new RelayCommand>(getpwd=> { service.Login(username, getpwd()); });
    

提交回复
热议问题