The difference is that an attached property applies to a class other than the one where it??™s
defined.
The most common example of attached properties is found in the layout containers
described in Chapter 4. For example, the Grid class defines the attached properties Row and
Column, which you set on the contained elements to indicate where they should be positioned.
Similarly, the DockPanel defines the attached property Dock, and the Canvas defines
the attached properties Left, Right, Top, and Bottom.
To define an attached property, you use the RegisterAttached() method instead of
Register(). Here??™s an example that registers the Grid.Row property:
Dim metadata As New FrameworkPropertyMetadata(0, _
AddressOf Grid.OnCellAttachedPropertyChanged)
Grid.RowProperty = DependencyProperty.RegisterAttached("Row", _
GetType(System.Int32), GetType(Grid), metadata, _
AddressOf Grid.IsIntValueNotNegative)
As with an ordinary dependency property, you can supply a FrameworkPropertyMetadata
object and a ValidateValueCallback.
When creating an attached property, you don??™t define the .NET property wrapper. That??™s
because attached properties can be set on any dependency object. For example, the Grid.Row
property may be set on a Grid object (if you have one Grid nested inside another) or on some
other element.
Pages:
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301