WPF PasswordChanged 属性 未检测退格删除

WPF PasswordChanged property not detecting backspace delete

我在密码框中使用更改密码 属性:

 PasswordChanged="NewPWBox_PasswordChanged"

用于检测 "real-time" 密码更改并更新我的其他控件,该控件显示密码的复杂程度。 它工作得很好。

唯一的问题是当我标记整个文本并用退格键删除它时,它没有检测到字符串的变化,我的其他控件也不会更新。

我真的不是前端人员,无法在任何地方找到解决这个问题的方法。

我显示密码复杂度的函数:

private void NewPWBox_PasswordChanged(object sender, RoutedEventArgs e)
    {

        string NewPass = NewPWBox.Password;

        PasswordScore passwordStrengthScore = PasswordAdvisor.CheckStrength(NewPass);

        switch (passwordStrengthScore)
        {
            case PasswordScore.VeryWeak:

                if(passwordStrengthScore == PasswordScore.VeryWeak)
                {
                    VW_Grid.Visibility = Visibility.Visible;
                    VW_Label.Visibility = Visibility.Visible;
                }

                //hide others
                Weak_Grid.Visibility = Visibility.Hidden;
                Weak_Label.Visibility = Visibility.Hidden;
                Medium_Grid.Visibility = Visibility.Hidden;
                Medium_Label.Visibility = Visibility.Hidden;
                Strong_Grid.Visibility = Visibility.Hidden;
                Strong_Label.Visibility = Visibility.Hidden;
                VS_Grid.Visibility = Visibility.Hidden;
                VS_Label.Visibility = Visibility.Hidden;

                break;
            case PasswordScore.Weak:

                if(passwordStrengthScore == PasswordScore.Weak)
                {
                    VW_Grid.Visibility = Visibility.Visible;
                    VW_Label.Visibility = Visibility.Visible;
                    Weak_Grid.Visibility = Visibility.Visible;
                    Weak_Label.Visibility = Visibility.Visible;
                }

                //hide others
                Medium_Grid.Visibility = Visibility.Hidden;
                Medium_Label.Visibility = Visibility.Hidden;
                Strong_Grid.Visibility = Visibility.Hidden;
                Strong_Label.Visibility = Visibility.Hidden;
                VS_Grid.Visibility = Visibility.Hidden;
                VS_Label.Visibility = Visibility.Hidden;

                break;
            case PasswordScore.Medium:

                if(passwordStrengthScore == PasswordScore.Medium)
                {
                    VW_Grid.Visibility = Visibility.Visible;
                    VW_Label.Visibility = Visibility.Visible;
                    Weak_Grid.Visibility = Visibility.Visible;
                    Weak_Label.Visibility = Visibility.Visible;
                    Medium_Grid.Visibility = Visibility.Visible;
                    Medium_Label.Visibility = Visibility.Visible;
                }

                //hide others
                Strong_Grid.Visibility = Visibility.Hidden;
                Strong_Label.Visibility = Visibility.Hidden;
                VS_Grid.Visibility = Visibility.Hidden;
                VS_Label.Visibility = Visibility.Hidden;

                break;
            case PasswordScore.Strong:

                if(passwordStrengthScore == PasswordScore.Strong)
                {
                    VW_Grid.Visibility = Visibility.Visible;
                    VW_Label.Visibility = Visibility.Visible;
                    Weak_Grid.Visibility = Visibility.Visible;
                    Weak_Label.Visibility = Visibility.Visible;
                    Medium_Grid.Visibility = Visibility.Visible;
                    Medium_Label.Visibility = Visibility.Visible;
                    Strong_Grid.Visibility = Visibility.Visible;
                    Strong_Label.Visibility = Visibility.Visible;
                }

                //hide others
                VS_Grid.Visibility = Visibility.Hidden;
                VS_Label.Visibility = Visibility.Hidden;

                break;
            case PasswordScore.VeryStrong:

                if(passwordStrengthScore == PasswordScore.VeryStrong)
                {
                    VW_Grid.Visibility = Visibility.Visible;
                    VW_Label.Visibility = Visibility.Visible;
                    Weak_Grid.Visibility = Visibility.Visible;
                    Weak_Label.Visibility = Visibility.Visible;
                    Medium_Grid.Visibility = Visibility.Visible;
                    Medium_Label.Visibility = Visibility.Visible;
                    Strong_Grid.Visibility = Visibility.Visible;
                    Strong_Label.Visibility = Visibility.Visible;
                    VS_Grid.Visibility = Visibility.Visible;
                    VS_Label.Visibility = Visibility.Visible;
                }
                break;
        }
    }

计算复杂度的方法如下:

    public enum PasswordScore
    {
        Blank = 0,
        VeryWeak = 1,
        Weak = 2,
        Medium = 3,
        Strong = 4,
        VeryStrong = 5
    }

    public class PasswordAdvisor
    {

        public static PasswordScore CheckStrength(string password)
        {
            int score = 1;
            if (password.Length < 1)
                return PasswordScore.Blank;
            if (password.Length < 4)
                return PasswordScore.VeryWeak;

            if (password.Length >= 8)
                score++;
            if (password.Length >= 12)
                score++;
            if (Regex.IsMatch(password, @"[0-9]+(\.[0-9][0-9]?)?", RegexOptions.ECMAScript))   //number only //"^\d+$" if you need to match more than one digit.
                score++;
            if (Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z]).+$", RegexOptions.ECMAScript)) //both, lower and upper case
                score++;
            if (Regex.IsMatch(password, @"[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]", RegexOptions.ECMAScript)) //^[A-Z]+$
                score++;
            return (PasswordScore)score;
        }

我认为你不需要它,只是为了确定我是否没有遗漏任何东西。

大家有什么想法吗?

谢谢并致以最诚挚的问候,

约翰

在所有闲聊评论之后: 在您的 switch 中放置一个 default case 并在该 case 中隐藏控件。

编辑:如果您将控件的数量减少到一个矩形和一个标签并更改颜色(如果需要,还可以更改大小,或渐变画笔...)和标签的文本,那就太好了。 .如果密码框为空则隐藏

它是如此简单、干净、面向未来并且与绑定和验证更加分离...