It seems odd to think of an older technology wrapping a newer
one, but that??™s how WPF is able to change a fundamental ingredient such as properties without
disrupting the rest of the .NET world.
Defining and Registering a Dependency Property
You??™ll spend much more time using dependency properties than creating them. However,
there are still many reasons that you??™ll need to create your own dependency properties. Obviously,
they??™re a key ingredient if you??™re designing a custom WPF element. However, they??™re also
required in some cases if you want to add data binding, animation, or another WPF feature to
a portion of code that wouldn??™t otherwise support it. For example, you??™ll see your first required
use of dependency properties in Chapter 9, when persisting custom information in a pagebased
application.
Creating a dependency property isn??™t difficult, but the syntax takes a little getting used to.
It??™s thoroughly different than creating an ordinary .NET property.
The first step is to define an object that represents your property. This is an instance of the
DependencyProperty class. The information about your property needs to be available all the
time, and possibly even shared among classes (as is common with WPF elements). For that
reason, your DependencyProperty object must be defined as a shared field in the associated
class.
Pages:
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286