1

I'm searching for a way to implement the InputScope of a Textbox like it is done in Wp7, but using classic WPF.

What I want to achieve is, that the input is restricted to only use decimal numbers. How can I achieve that?

<TextBox Text="{Binding Amount, Mode=TwoWay}" InputScope="Number"/>

InputScope is a valid attribute for classic WPF, but sadly it does not seem to work.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
BitKFu
  • 3,649
  • 3
  • 28
  • 43
  • 1
    Is this link any use http://stackoverflow.com/questions/1103765/how-to-define-textbox-input-restrictions ? – Phil Jun 07 '12 at 20:31

4 Answers4

7

InputScope doesn't force any kind of validation or restriction on the input. It is a hint to input processors (eg. on-screen keyboards, speech recognition) of the kind of data that can be entered in a control.

This value is used only if an IME (like an on-screen keyboard) is activated.

Even in WP7, InputScope doesn't restrict the values that can be entered in a text box. You could still enter unwanted characters if you could install an input processor that ignored InputScope.

If you want to restrict text input to specific characters you will have to use a MaskedTextBox or intercept keystroke events. The code will also be easier to understand.

It may be possible to use InputManager's PreProcessInput event to filter input events using the InputScope but it probably isn't worth the effort.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Do you have any idea how to utilize the PreProcessInput Handler? If you have a working solution for this, I would award you with the 50+ – BitKFu Jun 12 '12 at 12:51
  • The problem isn't how to work with PreProcessInput, it is how to filter the input using the InputScope. I don't know of a built-in way to do that. The answer in this question http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b657618e-7fc6-4e6b-9b62-1ffca25d186b/ shows how to create an Input event from another event, but you still can't filter the resulting event. – Panagiotis Kanavos Jun 12 '12 at 12:55
  • @BitKFu you could mark this as an answer as this answer was most usefull. – XAMlMAX Jun 17 '14 at 08:38
1

There is no built in way to do it. You'll need to write a bit of code to achieve what you're looking for. Here is an example of what you need to do: WPF Maskable TextBox for Numeric Values

Alex Gelman
  • 534
  • 3
  • 11
  • Hi Alex. Thanks for the Link, but this is not what I'm searching for. Even if I need to write some code, I would like to use the InputScope attribute which is already defined as a valid WPF attribute for the TextBox. – BitKFu Jun 04 '12 at 09:44
  • InputScope is a hint to input processors like on-screen keyboards, not a way to restrict input. You can't restrict keyboard events using InputScope – Panagiotis Kanavos Jun 12 '12 at 08:44
1

I haven't used InputScope, however, from the MSDN documentation (here, here, here and here) it appears that WPF's input scope requires a more complicated input e.g.

xmlns:swi should be mapped to System.Windows.Input.

<TextBox>
    <TextBox.InputScope>
        <swi:InputScope RegularExpression="^(0|(-(((0|[1-9]\d*)\.\d+)|([1-9]\d*))))$" />
    </TextBox.InputScope>
</TextBox>

(Regex string found on Google, I haven't checked it.)


Or perhaps:

<TextBox>
    <TextBox.InputScope>
        <swi:InputScopePhrase>
            <swi:InputScopePhrase.Names>Number</swi:InputScopePhrase>
        </swi:InputScopePhrase>
    </TextBox.InputScope>
</TextBox>
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
0
string txt = MessageWrite.Text;
        if (txt != "")
        {
            MessageWrite.Text = Regex.Replace(MessageWrite.Text, "[^a-f ^0-9]", "");
            if (txt != MessageWrite.Text)
            {
                MessageWrite.Select(MessageWrite.Text.Length, 0);
            }
        }

messageWrite is a textbox name. It restricts from A-F and 0-9. User can modify it :)

Owais Wani
  • 119
  • 1
  • 6
  • 14