Validations and dependency properties
DependencyProperty
supports the validation of the value being assigned to it via a callback method. Whenever the property value is set, the WPF and Silverlight runtimes can call a callback to check if its value is valid. The callback method simply needs to return true
for valid or false
for invalid to take advantage of this feature. This feature is especially useful for custom controls and element adapters, which are implemented as DependencyObject
. The callback is specified at the time that the dependency property is registered with the WPF runtime. The callback is passed as one of the parameters to the Register
static method on the DependencyProperty
class, as shown in the following code:
public static DependencyProperty Register(
string name,
Type propertyType,
Type ownerType,
PropertyMetadata typeMetadata,
ValidateValueCallback validateValueCallback
)
Where ValidateValueCallback
is as follows:
public delegate bool ValidateValueCallback( ...