It has nothing to do with Regular Expression. You need to handle the event
System.Windows.UIElement.TextInput
. This event can be canceled, which is done by assigned true to the property
System.Windows.RoutedEventArgs.Handled
(
runtime type will be different, see below) passed to your event handler. You will need to check up the value of the property
System.Windows.TextCompositionEventArgs.Text
, also passed in the same event arguments object and see if it is good for input or not. If not, assign true to
Handled
, to ignore the change. This text is supposed to be just one character the user tries to type.
Please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.textinput%28v=vs.110%29.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventhandler.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.text.aspx[
^].
Something like:
myTextBox.TextInput += (sender, eventArgs) => {
bool isGoodInput = CheckIfYouWantThisInput(eventArgs.Text);
eventArgs.Handled = !isGoodInput;
};
—SA