Validation Support
RadPropertyGrid supports validation through IDataErrorInfo, INotifyDataErrorInfo and through DataAnnotations as well as validating PropertySets.
Complete examples of the implementation of the validation mechanisms in the RadPropertyGrid control can be found in our GitHub repository:
You can run these examples through the SDK Samples Browser.
Validating through IDataErrorInfo
In order to have the validation rules defined through the IDataErrorInfo interface, you will have to implement additional code which sets the ValidatesOnDataErrors and NotifyOnValidationError properties of the Binding to "true". Please note, that the default binding generated for auto-generated PropertyDefinition have those properties set to "false".
Example 1: Setting ValidatesOnDataErrors and NotifyOnValidationError of the Binding
You can define the validation rules like so:
Example 2: Defining validation rules
Validating through INotifyDataErrorInfo
Provided your business objects correctly implement the INotifyDataErrorInfo interface, such validation is supported out of the box. Example 3 and Example 4 demonstrate a sample implementation.
Example 3: Defining the validation method
Example 4: Calling the ValidateName method
Validating through DataAnnotations
In order to have the validation rules defined through DataAnnotations respected, you will have to set the ValidatesOnExceptions property of the Binding to true. Please note, that the default binding generated for auto-generated PropertyDefinition does not have this property set to true.
Example 5: Setting ValidatesOnExceptions of Binding
For example you can define the Required DataAnnotations attribute like so:
Example 6: Defining DataAnnotations Attribute
You may notice that there is a call to a ValidateProperty method. You will have to define such method like this:
Example 7: Defining ValidateProperty method
Validating Property Sets
Before proceeding with this section you might find it useful to get familiar with RadPropertyGrid`s Property Sets functionality.
In order to enable validation for the fields that represent property sets, you need to set the PropertySetsValidationFunction of RadPropertyGrid. The function takes two arguments: string - the property that is being changed and object - the value that is about to be applied. The function returns a string that represents the error message.
Example 8: Setting the PropertySetsValidationFunction
Figure 1 shows the appearance of RadPropertyGrid after validation is applied.
Figure 1: Appearance of RadPropertyGrid after PropertySetsValidationFunction is set:
Both the ValidatesOnDataErrors and NotifyOnValidationError properties of the PropertyDefinition`s Binding should be set to True for the error message to appear.