How can I set a regular expression on WPF TextBox? I want the textbox to accept input in some predefined format. Is it possible?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- Improve converting string to readable urls
- Regex to match charset
You have several options:
ValidationRule
subclass (see below) and add it to your Binding's Validators propertyValidationCallback
on your bound property, throw an exception if the value is wrong, and use this technique for easily showing validation errorsFor arbitrary regexes I would generally use WPF's built-in validation features or do the validation on the bound property. For specific needs the PreviewKeyDown/PreviewTextInput or masked text box might be better.
Here is how you would create a ValidationRule subclass:
Either you can check at the changed event or you could use what's called a masked textbox.