我遇到了绑定到PasswordBox的问题。这似乎是一个安全风险,但我正在使用MVVM模式,所以我希望绕过这个。我在这里发现了一些有趣的代码(有人使用过这个或类似的代码吗?)
http://www.wpftutorial.net/PasswordBox.html
从技术上讲,它看起来很棒,但我不确定如何检索密码。
我基本上在我的LoginViewModel中有Username和Password的属性。Username很好,并且正在工作,因为它是TextBox。
我使用上面的代码,并输入这个
<PasswordBox ff:PasswordHelper.Attach="True"
ff:PasswordHelper.Password="{Binding Path=Password}" Width="130"/>
当我有PasswordBox作为TextBox和Binding Path=Password时,我的LoginViewModel中的属性被更新。
Button有一个Command。当我按下它时,CanLogin被调用,如果它返回true,则调用Login。< br / >
你可以看到,我在这里检查了属性Username,这很好
在Login中,我发送到我的服务Username和Password, Username包含来自我的View的数据,但Password是Null|Empty
private DelegateCommand loginCommand;
public string Username { get; set; }
public string Password { get; set; }
public ICommand LoginCommand
{
get
{
if (loginCommand == null)
{
loginCommand = new DelegateCommand(
Login, CanLogin );
}
return loginCommand;
}
}
private bool CanLogin()
{
return !string.IsNullOrEmpty(Username);
}
private void Login()
{
bool result = securityService.IsValidLogin(Username, Password);
if (result) { }
else { }
}
这就是我正在做的
<TextBox Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged}"
MinWidth="180" />
<PasswordBox ff:PasswordHelper.Attach="True"
ff:PasswordHelper.Password="{Binding Path=Password}" Width="130"/>
我有我的TextBox,这是没有问题的,但在我的ViewModel的Password是空的。
是我做错了什么还是漏了一步?
我放了一个断点,果然代码进入静态助手类,但它从来没有更新我的Password在我的ViewModel。