How to implement TextBox.InputScope in WPF - wpf

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.

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.

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

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>

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 :)

Related

IntegerUpDown Keyboard Input Ignored

I'm using the latest version of Xceed.Wpf.Toolkit.dll and I'm having an issue with the IntegerUpDown as well as the DoubleUpDown. I'm unable to enter values through the keyboard. Only the mouse is allowed to change values.
<xctk:IntegerUpDown x:Name="UpDownQuantity" Increment="1" Height="22" Width="85" ValueChanged="UpDownQuantityOnValueChanged"/>
Is there something specific I need to add to allow keyboard input either through number pad or number row on keyboard?
For those who may not read comments the op solved this himself :
#netniV the issue was I'm calling and showing the WPF window from WinForms. In order to pass the input to the WPF controls, you have to call ElementHost.EnableModelessKeyboardInterop(wpfwindow).

Prevent user from typing certain char in TextBox

I am using WPF, MVVM-Light.
In my UI I have a textbox, and I want to prevent the user from typing certain characters in the textbox.
I know if we use code-behind I could handle the key down keyPress events, can I achieve it through MVVM?
Can we use some behaviors or some interactivity triggers?
Using code-behind is perfectly OK with MVVM providing the code-behind is related to your View only.
So if you have some view-specific logic that says "User can only type numbers in this box", then it's perfectly OK to write a KeyPress event for the TextBox that only allows numeric keys to be processed. You could even throw this into a UserControl so it can be reusable.
However if your allowed character logic is based on application logic, such as "User can only use the characters defined in the app.config file for this string value", then you'd be better off validating that in the ViewModel.
Also note that restriction is different from validation.
If you want to validate a user's entry, then I would do so using IDataErrorInfo from the ViewModel layer, and possibly a binding with a mode of UpdateSourceTrigger=PropertyChanged so the validation is checked after every key press.
If you want to restrict what characters can be typed into a TextBox, then I would probably do that from the View layer in the code behind, as that is a functionality of the View.
Yes, to filter input the MVVM way, I would suggest either using a custom control (such as a masked TextBox control) or a Behavior.
I was recently looking for a good masked TextBox and there is a free one out there from Xceed which you can find here. I can't speak to this one, as I haven't used it, but I've been happy with other Xceed components I've used in the past.
However I didn't want to go third party and include a bunch of controls I didn't need, so I ended up creating a behavior that simply attaches to the TextBox and filters the input based on a FilterType. The behavior is pretty easy to create, and you simply use the PreviewTextInput event to filter out characters that you don't want.
This SO Answer has a number of suggestions and links to how to filter/mask the input and if you're not familiar with creating Attached Behaviors, this example shows how to create an Attached Behavior for a Masked Text Box.

How validate textbox (double,float type)

I need validate textbox.
range 0.0-200.0
and work with ',' and with '.'
How do this?
Use the NumericUpDown control in the Silverlight toolkit.
Don't try and reinvent the wheel with a textbox; using a control explicitly designed for this purpose is much more user-friendly. It's immediately obvious that they're expected to enter a number, they can easily change the current value using recognizable methods, and you're freed from having to write and test a bunch of custom validation code.

In WPF how can I duplicate the old WinForms OnKeyPressed functionality?

We have a WPF application where we need to detect if the user is typing something that can appear in a textbox. WinForms used to have a nice OnKeyPressed override that differed from OnKeyDown in that it was only called when a printable character was typed (e.g. it differentiated between 'A' and 'a' but not between the '1' on the top row vs. the numeric keypad. It also ignored keys like 'Shift', etc.) We need to duplicate that functionality in WPF.
Now I know I can override OnKeyDown and inspect the keycode, but that means manually testing for every possible key that would result in something appearing in a textbox. That seems to be the consensus from the other similar SO questions that I've found, but I just can't believe something so basic would be left out of the API.
Asides from a monster if-tree, one 'hacky' solution is to use an in-memory textbox, pass the keycode to it, then inspect if the text has a length. But I'm getting nauseous even typing that as a possible solution! UGH!! I feel dirty!
SO... what's the easiest way to tell if a user pressed a key or keys that will result in a printable character?
TIA,
Mark
Actually, I think I just found it so I'm posting the answers for others. You don't use any 'Key' events at all. WPF more clearly separates those out from the textual values. As such, you override the OnPreviewTextInput and/or OnTextInput functions. To get what was actually typed, check the .Text property on the event args which returns a simple string.
Done and done!
Hope this helps others! Been driving me up a wall!

Make the official WPF RibbonTextBox look like a PasswordBox

I'm developing an application with the official WPF Ribbon system, and I'd like to have a password field up in the ribbon. Is there any way I can modify the RibbonTextBox control to display dots instead of characters, the way a PasswordBox does? Thanks in advance.
Can't you just use the key up event, and take the value out of the box, store it in a variable, and replace it with the character you want?
User presses "A" on key up, you grab the value, store it in a variable, and then replace it with an "*". Keep doing this until they stop typing in the box?

Resources