For example, the FrameworkElement class defines a Margin property that all elements
share. Unsurprisingly, Margin is a dependency property. That means it??™s defined in the
FrameworkElement class like this:
Public Class FrameworkElement
Inherits UIElement
Implements ...
Public Shared ReadOnly MarginProperty As DependencyProperty
...
End Class
By convention, the field that defines a dependency property has the name of the ordinary
property, plus the word Property at the end. That way, you can separate the dependency property
definition from the name of the actual property. The field is defined with the ReadOnly
keyword, which means it can only be set in the shared constructor for the FrameworkElement.
Defining the DependencyProperty object is just the first step. In order for it to become
usable, you need to register your dependency property with WPF. This step needs to be completed
before any code uses the property, so it must be performed in a shared constructor for
the associated class.
WPF ensures that DependencyProperty objects can??™t be instantiated directly, because the
DependencyObject class has no public constructor. Instead, a DependencyObject instance
can be created only using the shared DependencyProperty.Register() method. WPF also
ensures that DependencyProperty objects can??™t be changed after they??™re created, because all
DependencyProperty members are read-only.
Pages:
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287