Prev | Current Page 278 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

IsValid(True, False, True, False)
End Function
There??™s one limitation with validation callbacks: they are shared methods that don??™t have
access to the object that??™s being validated. All you get is the newly applied value. While that
makes them easier to reuse, it also makes it impossible to create a validation routine that takes
other properties into account. The classic example is an element with a Maximum and Minimum
property. Clearly, it should not be possible to set the Maximum to a value that??™s less than
the Minimum. However, you can??™t enforce this logic with a validation callback because you??™ll
only have access to one property at a time.
nNote The preferred approach to solve this problem is to use value coercion. Coercion is a step that
occurs just before validation, and it allows you to modify a value to make it more acceptable (for example,
raising the Maximum so it??™s at least equal to the Minimum) or disallow the change altogether. The coercion
step is handled through another callback, but this one??™s attached to the FrameworkPropertyMetadata object,
which is described in the next section.
The Property Wrapper
The final step is to wrap your WPF property in traditional .NET property. However, whereas
typical property procedures retrieve or set the value of a private field, the property procedures
for a WPF property use the GetValue() and SetValue() methods that are defined in the base
DependencyObject class.


Pages:
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290