我对这个方法的细节很感兴趣。 如何以及通过什么来跟踪属性(property)的值(value)变化?
请您参考如下方法:
Andrew Smith 有一个 cool blog post关于它,我认为它会对您有所帮助。
The solution that I have seen given for this is to get to the
PropertyDescriptorand use itsAddValueChangedmethod to provide anEventHandlerto receive a notification when the property has changed. Sometimes, the reply will mention/useDependencyPropertyDescriptordirectly but its the same thing since that is just a derived PropertyDescriptor that provides additional information about the underlying DependencyProperty it represents. You can get to this property descriptor in a few ways but the most common are to get it from the TypeDescriptor.GetProperties method or using the DependencyPropertyDescriptor.FromProperty.The issue with this approach is that it will root your object so it will never get collected by the GC. There have been plenty of discussions about how hooking events (particularly static events) can root your object so I won’t go into great detail there. While it does not seem that you are hooking a static event in this case, in essence you are. When you add a handler to a property descriptor, that property descriptor stores the delegate in a hashtable keyed by the object whose property you are hooking. A delegate/handler is basically a pointer to a method on an object (or no object if its for a static method) so that means the property descriptor has a reference to your object as well as the object whose value you are watching (since that is the key into the hashtable). The property descriptors themselves are cached statically so the hashtable is kept around and therefore your object and the one you are watching are as well.
