How to set up a TextBox
to get only certain values. e.g. DateTime
input box with defined format settings.
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
How about using the Binding Validation that comes with the WPF Framework.
Create a ValidationRule like so
Then add it to your binding in xaml as well as a style to handle when the field is invalid. (You can also use Validation.ErrorTemplate if you're inclined to completely change the control.) This one puts the ValidationResult text as a tooltip and the box to red.
A recommendation would be to take the style and put it into a resource dictionary so any textbox you want to have the same appearance when its own validation fails. Makes the XAML a lot cleaner too.
You can also override the input methods on the textbox and evaluate the input at that point. It all depends on your architecture.
Some I've overridden before for this kind of task:
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
And a function. This one just checks the string has content. Yours will be more complex depending on the exact format you want to enforce:
Try using a MaskedTextBox.
It has things like DateTime defined format, and some more.