Check PasswordBox if user type anything in WPF

为君一笑 提交于 2019-12-11 11:07:27

问题


I am using PasswordBox and 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?

It behaves differently from TextBox since you can't bind it to text and when user types anything raises some event. Any idea?

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>

回答1:


You can use the PasswordChanged event via Interactions like this:

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

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

PasswordBox in WPF Tutorial

Binding to PasswordBox in WPF (using MVVM)

How to bind to a PasswordBox in MVVM




回答2:


You can use PasswordChanged event which fires when the string in the passwordbox changes:

XAML Part:

<PasswordBox Name="pwdBox" PasswordChanged="pwdBox_PasswordChanged" />
<Button Name="someButton" IsEnabled="False" Click="someClickEvent" />

C# Part:

    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

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.

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



来源:https://stackoverflow.com/questions/22127922/check-passwordbox-if-user-type-anything-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!