15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1060 ❘ ChaPTer 36 business ApplicAtiOns with wpf<br />

With a .<strong>NET</strong> entity class, it would not be clear what an indexer would return; for<br />

example, what would you expect from an object of type Person calling an indexer<br />

That’s why it is best to do an explicit implementation of the interface IDataErrorInfo.<br />

This way this indexer can be accessed only by using the interface, <strong>and</strong> the .<strong>NET</strong> class<br />

could do a different implementation for other purposes.<br />

If you set the property ValidatesOnDataErrors of the Binding class to true , the interface<br />

IDataErrorInfo is used during binding. Here, when the TextBox is changed, the binding mechanism<br />

invokes the indexer of the interface <strong>and</strong> passes Value2 to the columnName variable:<br />

< Label Margin="5" Grid.Row="1" Grid.Column="0" > Value2: < /Label ><br />

< TextBox Margin="5" Grid.Row="1" Grid.Column="1"<br />

Text="{Binding Path=Value2, ValidatesOnDataErrors=True}" / ><br />

code snippet ValidationDemo/MainWindow.xaml<br />

Custom Validation rules<br />

To get more control of the validation you can implement a custom validation rule. A class implementing<br />

a custom validation rule needs to derive from the base class ValidationRule . With the previous<br />

two examples, validation rules have been used as well. Two classes that derive from the abstract<br />

base class ValidationRule are DataErrorValidationRule <strong>and</strong> ExceptionValidationRule .<br />

DataErrorValidationRule is activated by setting the property ValidatesOnDataErrors <strong>and</strong> uses the<br />

interface IDataErrorInfo ; ExceptionValidationRule deals with exceptions <strong>and</strong> is activated by setting<br />

the property ValidatesOnException .<br />

Here a validation rule is implemented to verify for a regular expression. The class<br />

RegularExpressionValidationRule derives from the base class ValidationRule <strong>and</strong> overrides the<br />

abstract method Validate() that is defi ned by the base class. With the implementation, the RegEx class<br />

from the namespace System.Text.RegularExpressions is used to validate the expression defi ned by the<br />

Expression property:<br />

public class RegularExpressionValidationRule: ValidationRule<br />

{<br />

public string Expression { get; set; }<br />

public string ErrorMessage { get; set; }<br />

}<br />

public override ValidationResult Validate(object value,<br />

CultureInfo cultureInfo)<br />

{<br />

ValidationResult result = null;<br />

if (value != null)<br />

{<br />

Regex regEx = new Regex(Expression);<br />

bool isMatch = regEx.IsMatch(value.ToString());<br />

result = new ValidationResult(isMatch, isMatch <br />

null: ErrorMessage);<br />

}<br />

return result;<br />

}<br />

Regular expressions are explained in Chapter 9.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!