WPF Input Validation presented by Jonathan Birkholz
About Me Blogs : theabsentmindedcoder.com wizardsofsmart.net Twitter : RookieOne GitHub : github.com/RookieOne Email : rookieone@gmail.com
Virtual Brown Bags What : Virtual Brown Bags An online meeting where the attendees share: Tips and tricks Tools, shortcuts, articles, books, patterns, languages, you name it Experiences Things they’ve learned the hard way Frustrations or difficulties Frustrating issues or difficulties they’re facing that somebody else may be able to help them with When : Every Thursday @ 12pm – 1pm Where : http://snipr.com/virtualaltnet Who : Anyone and Everyone
EPS Consulting Custom Software Development Consulting / Mentoring Training WPF .NET CODE Magazine Hiring Developers, PM’s
If you know all this… then feel free to leave How to do binding validation with: Validation Rules Exceptions IDataErrorInfo Enterprise Library - Validation Application Block Markup Extensions
Objective Not to tell you how to do input validation with WPF Instead just show you a variety of methods available In the end, you must decide which methods works best for you
Sample App to create a list of party invitees Rules for being an Invitee Name is required Age has to be greater than 18 Must have a valid email address
Using Validation Rules
Validation Rules Validation Rules are the mechanism built into data binding with WPF
Binding – Markup Extension Normally you see binding as but that is just a binding markup extension. We can also set a binding like this
Add Validation Rules to Binding Binding needs to Validate on Data Error Allows the setting of Validation Rules to Binding
What Validation Rules come with WPF
DataErrorValidationRule
ExceptionValidationRule
Custom Validation Rules Just inherit from Validation Rule And override Validate method …
Implement Validate and return ValidationResult Fail Pass Validation Error Message
Required
Greater Than
Regular Expression
Displaying the Errors
Error Template Creates the red border
Displaying Error Message This is setting the error template we specified This trigger sets the tooltip to the error message
Tooltip Conflicts Now my normal tooltip shows Now I loss my Error Message tooltip This is due to how Property values are resolved … Styles, Triggers, local, etc
Using Exceptions
On source… Throw exception
On binding… Set ValidatesOnExceptions to True
Exception Messages Hmm… not exactly the error message we would want… And this message is lame…
Using IDataErrorinfo
Implement
On base ViewModel
On Party Invitee View Model
On Binding Validates On Data Errors
Works great right? Not quite so perfect…. Bad input… no error
Solution Make Age a string Change Error Checking
And now…
Enterprise Library
Enterprise Library is The Microsoft Enterprise Library is a collection of application blocks designed to assist developers with common enterprise development challenges One of which is the Validation Block
You can get EntLib 4.1 here http://msdn.microsoft.com/en-us/library/dd203099.aspx
Add Reference
Use Validation Attributes
Error Provider Martin Bennedik @ http://www.bennedik.de/2007/04/wpf-integration-for-validation.html Download WPF Validation project Has the ErrorProvider control The error provider recursively finds all the bindings down the Logical Tree and adds the Enterprise Validation Rule to the bindings
On View Wrap bindings to validate with the Error Provider Bindings can remain simple
In action Not exactly the best out of the box messages…
Setting the Error Message
New and improved
Custom markup extension
Markup Extensions The most common markup extensions used in WPF programming are those that support resource references (StaticResource and DynamicResource), and those that support data binding (Binding) Static Resource Dynamic Resource Binding
Custom Markup Extensions We can make our own markup extensions Use attribute to specify markup extension return type Inherit from MarkupExtension Implement ProvideValue
Binding Decorator Base Philip Sumi @ http://www.hardcodet.net/2008/04/wpf-custom-binding-class
Base Validation Binding
Required Implementation Xaml
Greater Than Implementation Xaml
Email Implementation Xaml
With Custom Markup Extensions
Conclusion We covered how we can do WPF Input Validation with Validation Rules Exceptions IDataErrorInfo Enterprise Library Validation Block Custom Markup Extensions
Questions ?
Git Hub Repository http://github.com/RookieOne/WPF-Input-Validation Has solution with projects and slide show Offered as is
References MSDN http://msdn.microsoft.com/en-us/library/dd203099.aspx Blogs and articles by Josh Smith Karl Shifflet Martin Bennedik Philip Sumi
Presentation I gave at the Houston TechFest Sept 20 more
Presentation I gave at the Houston TechFest Sept 2009. Covers WPF Input Validation using Validation Rules, Exceptions, IDataErrorInfo, Enterprise Library, and Custom Markup Extensions less
0 comments
Post a comment