简体   繁体   English

当输入是Button.Content而不是键盘时,如何在PasswordBox中设置MaxLength? (WPF)

[英]How to set MaxLength in PasswordBox when input is Button.Content instead of keyboard? (WPF)

I have a simple keypad with 0-9, a Clear button, and an Enter Button. 我有一个带有0-9的简单键盘,一个清除按钮和一个输入按钮。 Clicking the numbers places the content into a PasswordBox. 单击数字将内容放入PasswordBox。 Clicking the Clear button simulates a backspace, and the Enter Button acts as a submit for predefined 4 digit codes. 单击“清除”按钮将模拟退格,并且“输入”按钮充当预定义4位数字代码的提交。 If the correct code is entered, it appends to a textbox showing that access was granted, else, access denied. 如果输入正确的代码,它将附加到一个文本框,显示已授予访问权限,否则,访问被拒绝。

WPF应用程序

I am having no problems except when trying to set a MaxLength on the PasswordBox. 除了尝试在PasswordBox上设置MaxLength之外,我没有任何问题。 I have tried it in my XAML with: 我已经在XAML中使用以下方法进行了尝试:

<PasswordBox PasswordChanged="securityCodeTextBox_PasswordChanged" MaxLength="4" KeyDown="securityCodeTextBox_KeyDown" x:Name="securityCodeTextBox" PasswordChar="•" HorizontalAlignment="Left" Margin="117,20,0,0" VerticalAlignment="Top" Height="23" Width="213"/>

I've also tried it programmatically with: 我还通过以下方式通过程序进行了尝试:

securityCodeTextBox.MaxLength = 4;

This is the only code in my function for the numeric buttons: 这是我的函数中数字按钮的唯一代码:

private void ButtonClickNum(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;

        securityCodeTextBox.Password += button.Content.ToString();
    }

I don't have to implement this, since if the code is wrong, it will just log "Access Denied" in the textbox below it. 我不必执行此操作,因为如果代码错误,它将仅在其下面的文本框中记录“访问被拒绝”。 However, I am really curious on how to do this after realizing the MaxLength is for keyboard input , not button clicks. 但是,我对实现MaxLength是用于键盘输入而不是按钮单击之后的操作感到非常好奇。 I currently have the keyboard completely disabled with: 我目前使用以下命令完全禁用了键盘:

//Prevent keyboard input

    private void securityCodeTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
    }

Next attempt 下次尝试

I created a very sloppy way to disable all but the Clear and Enter buttons if the length reaches >=4 with these code snippets in the Num and Clear Button Click functions: 我创建了一个非常草率的方法,如果长度达到> = 4,则禁用除“清除”和“输入”按钮之外的所有按钮,并在Num和“清除按钮的单击”功能中使用以下代码段:

private void ButtonClickNum(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;

        securityCodeTextBox.Password += button.Content.ToString();

        //Disable all buttons if MaxLength is reached

        if(securityCodeTextBox.Password.Length >= 4)
        {
            button0.IsEnabled = false;
            button1.IsEnabled = false;
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            button4.IsEnabled = false;
            button5.IsEnabled = false;
            button6.IsEnabled = false;
            button7.IsEnabled = false;
            button8.IsEnabled = false;
            button9.IsEnabled = false;
        }

    }

Clear Button 清除按钮

    /**
     * Remove(int startIndex, int count) startIndex = position, count = num of chars to delete
     */
    private void ButtonClickClear(object sender, RoutedEventArgs e)
    {

        if (securityCodeTextBox.Password.Length > 0)
        {
            securityCodeTextBox.Password = securityCodeTextBox.Password.Remove(securityCodeTextBox.Password.Length - 1, 1);
        }
        //Enable all the buttons again once password is less than MaxLength

        if (securityCodeTextBox.Password.Length < 4)
        {
            button0.IsEnabled = true;
            button1.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            button4.IsEnabled = true;
            button5.IsEnabled = true;
            button6.IsEnabled = true;
            button7.IsEnabled = true;
            button8.IsEnabled = true;
            button9.IsEnabled = true;
        }
    }

Is there a cleaner way to implement the MaxLength method when the input is the content of a button? 当输入是按钮的内容时,是否存在一种更干净的方法来实现MaxLength方法?

I never worked with WPF or password input Controls before, but I would try it like this, it just wont add up the password if the length is reached. 我以前从未使用过WPF或密码输入控件,但我会这样尝试,如果达到长度,它将不会添加密码。

    private void ButtonClickNum(object sender, RoutedEventArgs e)
    {
        Button button = (Button)sender;

        if(securityCodeTextBox.Password.Length < 4)
            securityCodeTextBox.Password += button.Content.ToString();             
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM