我遇到了绑定到PasswordBox的问题.这似乎是一个安全风险,但我正在使用MVVM模式,所以我希望绕过这个.我在这里找到了一些有趣的代码(有没有人用过这个或类似的东西?)
http://www.wpftutorial.net/PasswordBox.html
它在技术上看起来很棒,但我不确定如何检索密码.
我基本上有我的LoginViewModelfor Username和属性Password.Username很好,正在工作,因为它是一个TextBox.
我按照说明使用了上面的代码并输入了这个
<PasswordBox ff:PasswordHelper.Attach="True"
ff:PasswordHelper.Password="{Binding Path=Password}" Width="130"/>
Run Code Online (Sandbox Code Playgroud)
当我有了PasswordBoxa TextBox,Binding Path=Password然后我的房产LoginViewModel被更新了.
我的代码很简单,基本上我有一个Commandfor my Button.当我按下它时会CanLogin被调用,如果它返回true,则调用它Login.
你可以看到我检查我的房产在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
{ …Run Code Online (Sandbox Code Playgroud) 我怎样才能在XAML中执行此操作:
伪代码:
<TextBox Text="{Binding Password}" Type="Password"/>
Run Code Online (Sandbox Code Playgroud)
这样当用户输入密码时,用户就会看到星星或圆点.
我已经尝试了各种示例,建议使用PasswordChar和PasswordBox,但无法使这些工作.
例如,我可以做到这一点,如图在这里:
<PasswordBox Grid.Column="1" Grid.Row="1"
PasswordChar="*"/>
Run Code Online (Sandbox Code Playgroud)
但我当然希望将Text属性绑定到我的ViewModel,这样我可以在单击按钮时发送绑定TextBox的值(不使用后面的代码),我想这样做:
<TextBox Grid.Column="1" Grid.Row="0"
Text="{Binding Login}"
Style="{StaticResource FormTextBox}"/>
<PasswordBox Grid.Column="1" Grid.Row="1"
Text="{Binding Password}"
PasswordChar="*"
Style="{StaticResource FormTextBox}"/>
Run Code Online (Sandbox Code Playgroud)
但是PasswordBox没有Text属性.
http://caliburnmicro.com上的Caliburn.Micro主页提出了以下声明,但是我无法使用我从这个示例中可以想到的任何变体使用CMBox控件.不管怎么说这都不会起作用,因为这些名字并不相同.有没有人有CM示例允许我获得PasswordBox的值?是否需要特定版本的CM?我正在运行CM的1.5.2版本.理想情况下不使用附加属性,但如果可以使用CM,那么唯一的方法就好了.请不要在安全问题上讲课,因为这不是我的问题.
使用参数和保护方法自动在视图和视图模型之间应用方法
<StackPanel>
<TextBox x:Name="Username" />
<PasswordBox x:Name="Password" />
<Button x:Name="Login" Content="Log in" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
public bool CanLogin(string username, string password)
{
return !String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password);
}
public string Login(string username, string password)
{
...
}
Run Code Online (Sandbox Code Playgroud)