Sự khác biệt lớn giữa tài sản gắn liền và tài sản phụ thuộc thường xuyên được minh họa trong hình 7-10. Chỉ có sự khác biệt lớn cấu trúc giữa các thuộc tính phụ thuộc thường xuyên và tài sản gắn liền như sau: • • các thuộc tính phụ thuộc thường xuyên có một wrapper tài sản CLR xung quanh cuộc gọi của họ để getValue và setValue. | CHAPTER 7 DEPENDENCY PROPERTIES The Structure of an Attached Property The major differences between attached properties and regular dependency properties are illustrated in Figure 7-10. The only major structural differences between regular dependency properties and attached properties are the following Regular dependency properties have a CLR property wrapper around their calls to GetValue and SetValue. Attached properties have two static methods of the form GetXXX and SetXXX where XXX is the name of the dependency property field without the Property suffix. - These methods call the GetValue and SetValue methods of the target object. - The target object must be derived from Dependencyobject in order for the GetXXX and SetXXX methods to call its GetValue and SetValue methods. Besides these differences attached properties are registered with the static RegisterAttached method rather than the Register method. Figure 7-10. Differences between regular dependency properties and attached properties 189 CHAPTER 7 DEPENDENCY PROPERTIES Creating an Attached Property This section shows a simple program that creates an attached property called Countproperty which represents an object of type int. This property has no special dependency property characteristics beyond storing an int. The target objects are objects of the following class called IntStorage. Notice that there s nothing in the class itself other than what it inherits from Dependencyobject and all target objects must derive from DependencyObject . public class IntStorage DependencyObject . . . - . L . Must Derive from Dependencyobject The Window1 class sets up the structure for an attached property in the following manner It declares the DependencyProperty CountProperty. The attached property is registered using the RegisterAttached method in the static constructor. The two access methods GetCount and SetCount are declared and call the GetValue and SetValue methods on the instances of the objects using the attached