如果用户在 WPF 中输入任何内容,请检查 PasswordBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22127922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check PasswordBox if user type anything in WPF
提问by Jean Tehhe
I am using PasswordBoxand I want to detect whenever the user typed there anything, if yes I need to change Button status to enabled. How can I check if user types anything
in the PasswordBox?
我正在使用PasswordBox并且我想检测用户何时在那里输入任何内容,如果是,我需要将按钮状态更改为启用。如何检查用户是否在PasswordBox.
It behaves differently from TextBoxsince you can't bind it to text
and when user types anything raises some event. Any idea?
它的行为不同于TextBox因为您无法将其绑定到文本并且当用户键入任何内容时会引发某些事件。任何的想法?
I have tried with the code below, but I get errors:
我已尝试使用以下代码,但出现错误:
<PasswordBox>
<i:Interaction.Triggers>
<EventTrigger EventName="KeyDown">
<si:InvokeDataCommand Command="{Binding MyCommand}" />
</EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
回答by mcy
You can use PasswordChangedevent which fires when the string in the passwordbox changes:
您可以使用PasswordChanged当密码框中的字符串更改时触发的事件:
XAML Part:
XAML 部分:
<PasswordBox Name="pwdBox" PasswordChanged="pwdBox_PasswordChanged" />
<Button Name="someButton" IsEnabled="False" Click="someClickEvent" />
C# Part:
C#部分:
private void pwdBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if(String.IsNullOrWhiteSpace(pwdBox.Password)
somebutton.IsEnabled = false;
else
somebutton.IsEnabled = true;
}
Please note that MSDN says
请注意 MSDN 说
When you get the Password property value, you expose the password as plain text in memory. To avoid this potential security risk, use the SecurePassword property to get the password as a SecureString.
当您获得 Password 属性值时,您将密码以纯文本形式显示在内存中。为避免这种潜在的安全风险,请使用 SecurePassword 属性以 SecureString 形式获取密码。
Therefore the following code may be preferred:
因此,以下代码可能是首选:
private void pwdBox_PasswordChanged(object sender, RoutedEventArgs e)
{
if (pwdBox.SecurePassword.Length == 0)
{
btn.IsEnabled = false;
}
else
{
btn.IsEnabled = true;
}
}
If you only have access to viewModel, then you may use attached properties such that you create a bindable password or securepassword, as in this example
如果您只能访问 viewModel,那么您可以使用附加属性来创建可绑定的密码或安全密码,如本例所示
回答by Anatoliy Nikolaev
You can use the PasswordChangedevent via Interactionslike this:
您可以PasswordChanged通过以下方式使用该事件Interactions:
XAML
XAML
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<PasswordBox BorderBrush="#FFB0B1AB"
Width="100"
Height="25"
VerticalAlignment="Bottom">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PasswordChanged">
<i:InvokeCommandAction Command="{Binding PasswordChangedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>
RelayCommand
RelayCommand
private ICommand _passwordChangedCommand = null;
public ICommand PasswordChangedCommand
{
get
{
if (_passwordChangedCommand == null)
{
_passwordChangedCommand = new RelayCommand(param => this.PasswordChanged(), null);
}
return _passwordChangedCommand;
}
}
private void PasswordChanged()
{
// your logic here
}
Some useful links
Some useful links
Binding to PasswordBox in WPF (using MVVM)
Binding to PasswordBox in WPF (using MVVM)

