Windows Phone PasswordBox with numeric Input Scope

First, define below variables at class level

string_enteredPasscode="";
string_passwordChar="*";

 

Second, trigger TextBox KeyUp event that you use in xaml,

 privatevoidTextBox_KeyUp(objectsender, System.Windows.Input.KeyEventArgse)
        {
            if (tbCvv.Text.Length==3) return;
            _enteredPasscode=GetNewPasscode(_enteredPasscode, e);
            tbCvv.Text=Regex.Replace(_enteredPasscode, @".", _passwordChar);
 
            tbCvv.SelectionStart=tbCvv.Text.Length;
        }
 
        privatestringGetNewPasscode(stringoldPasscode, KeyEventArgskeyEventArgs)
        {
            stringnewPasscode=string.Empty;
            switch (keyEventArgs.Key)
            {
                caseKey.D0:
                caseKey.D1:
                caseKey.D2:
                caseKey.D3:
                caseKey.D4:
                caseKey.D5:
                caseKey.D6:
                caseKey.D7:
                caseKey.D8:
                caseKey.D9:
                    newPasscode=oldPasscode+ (keyEventArgs.PlatformKeyCode-48);
                    break;
                caseKey.Back:
                    if (oldPasscode.Length>0)
                        newPasscode=oldPasscode.Substring(0, oldPasscode.Length-1);
                    break;
                default:
                    //others
                    newPasscode=oldPasscode;
                    break;
            }
            returnnewPasscode;
        }

Add comment