How can I create a PasswordBox with an input scope that accepts only numbers?
Asked Answered
S

3

5

This seems like a glaring oversight in Windows Phone. The <PasswordBox /> control doesn't allow for specifying InputScope.

I need to show the PasswordBox as part of a custom password entry screen (e.g. images and buttons), and show a numeric keypad to allow only numeric input.

Coding4Fun's PasswordInputPrompt allows for numeric input but it's not customizable, in that I can't build a custom layout around it, it only allows for Title & Message values.

Is there any way for me to do this?

Swayback answered 3/7, 2014 at 9:19 Comment(4)
developer.nokia.com/community/wiki/…Machicolate
Coding4Fun's libraries and the PasswordInputPrompt is completely open source, you are definitely able to modify and adapt it to your needs. Perhaps if you specified what your custom layout should entail, you might get some help.Prance
@HeenaPatil Did you try that nokia sample?Murage
@anonshankar I got the Nokia example working.. it seems to be doing the trick.Swayback
S
7

Your best off just creating your own user control.

The xaml would look something like:

<Grid x:Name="LayoutRoot">
    <TextBox x:Name="PasswordTextBox" InputScope="Number" MaxLength="{Binding MaxLength, ElementName=UserControl}" KeyUp="PasswordTextBox_KeyUp"/>
</Grid>

and the code behind could be something like:

public partial class NumericPasswordBox : UserControl
{
    #region Password 

    public string Password
    {
        get { return (string)GetValue(PasswordProperty); }
        set { SetValue(PasswordProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.Register("Password", typeof(string), typeof(NumericPasswordBox), new PropertyMetadata(null));

    #endregion

    #region MaxLength

    public int MaxLength
    {
        get { return (int)GetValue(MaxLengthProperty); }
        set { SetValue(MaxLengthProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MaxLength.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty MaxLengthProperty =
        DependencyProperty.Register("MaxLength", typeof(int), typeof(NumericPasswordBox), new PropertyMetadata(100));

    #endregion

    public NumericPasswordBox()
    {
        InitializeComponent();
    }

    private void PasswordTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        Password = PasswordTextBox.Text;

        //replace text by *
        PasswordTextBox.Text = Regex.Replace(Password, @".", "●");

        //take cursor to end of string
        PasswordTextBox.SelectionStart = PasswordTextBox.Text.Length;
    }
}

You can customize all you want from then on however you wish, using dependency properties like MaxLength, shown in the example.

Staw answered 3/7, 2014 at 16:42 Comment(2)
Looking at the code, this simply cannot work. The password property would be filled with ●Colorant
This is pretty awesome! It solved my Prob. Thanks buddy . +1Morelli
A
0

Sadly no, the PasswordBox control doesn't inherit from the TextBox control so it doesn't support InputScope. I think the best way for you is to create a new control.

I agree with you, and it's kind of frustrating as Windows Phone already has that type of control (lock screen password).

Amnesia answered 3/7, 2014 at 9:41 Comment(0)
F
0

Please feel free to try my new and easy-to-use NumericPassword component for Windows Phone.

It is based on basic online samples but has a better support for value editing in GUI (e.g. caret positioning, overwriting selected parts) to provide better user experience.

A quick tutorial can be found here.

Freezedrying answered 28/8, 2015 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.