如果用户在 WPF 中输入任何内容,请检查 PasswordBox [英] Check PasswordBox if user type anything in WPF

查看:83
本文介绍了如果用户在 WPF 中输入任何内容,请检查 PasswordBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PasswordBox 并且我想在用户输入任何内容时进行检测,如果是,我需要将 Button 状态更改为启用.如何检查用户是否输入任何内容在 PasswordBox?

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?

它的行为与 TextBox 不同,因为你不能将它绑定到文本当用户输入任何内容时会引发一些事件.有什么想法吗?

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>

推荐答案

您可以使用 PasswordChanged 事件通过 Interactions 像这样:

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
}

一些有用的链接

WPF 教程中的密码框

绑定到WPF中的PasswordBox(使用MVVM)

如何绑定到 MVVM 中的 PasswordBox

这篇关于如果用户在 WPF 中输入任何内容,请检查 PasswordBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆