(The attached
properties that were discussed earlier are a special type of dependency property.)
In fact, the Grid.SetRow() method is actually a shortcut that??™s equivalent to calling
DependencyObject.SetValue() method, as shown here:
txtQuestion.SetValue(Grid.RowProperty, 0)
Attached properties are a core ingredient of WPF. They act as an all-purpose extensibility
system. For example, by defining the Row property as an attached property, you guarantee
that it??™s usable with any control. The other option, making it a part of a base class such as
FrameworkElement, complicates life. Not only would it clutter the public interface with properties
that only have meaning in certain circumstances (in this case, when an element is being
used inside a Grid), it also makes it impossible to add new types of containers that require
new properties.
CHAPTER 2 n XAML 38
nNote Attached properties are very similar to extender providers in a Windows Forms application. Both
allow you to add ???virtual??? properties to extend another class. The difference is that you must create an
instance of an extender provider before you can use it, and the extended property value is stored in the
extender provider, not the extended control. The attached property design is a better choice for WPF because
it avoids lifetime management issues (for example, deciding when to dispose of an extender provider).
Pages:
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141