Prev | Current Page 279 | Next

Matthew MacDonald

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

Here??™s an example:
Public Property Margin As Thickness
Get
Return CType(GetValue(MarginProperty),Thickness)
End Get
Set
SetValue(MarginProperty, value)
End Set
End Property
When you create the property wrapper, you should include nothing more than a call to
SetValue() and a call to GetValue(), as in the previous example. You should not add any extra
code to validate values, raise events, and so on. That??™s because other features in WPF may
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 142
bypass the property wrapper and call SetValue() and GetValue() directly. (One example is
when a compiled XAML file is parsed at runtime.) Both SetValue() and GetValue() are public.
nNote The property wrapper isn??™t the right place to validate data or raise an event. However, WPF does
provide a place for this code??”the trick is to use dependency property callbacks. Validation should be performed
through the DependencyProperty.ValidateValueCallback shown previously, while events can be raised
from the FrameworkPropertyMetadata.PropertyChangedCallback shown in the next section.
You now have a fully functioning dependency property, which you can set just like any
other .NET property using the property wrapper:
myElement.Margin = New Thickness(5)
There??™s one extra detail. Dependency properties follow strict rules of precedence to determine
their current value.


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