听取依赖财产的变化 [英] Listen to changes of dependency property
问题描述
有没有办法听取 DependencyProperty
的更改?我想要通知并在值更改时执行一些操作,但我无法使用绑定。它是另一个类的 DependencyProperty
。
Is there any way to listen to changes of a DependencyProperty
? I want to be notified and perform some actions when the value changes but I cannot use binding. It is a DependencyProperty
of another class.
推荐答案
code> DependencyProperty 一个单独的类,最简单的方法是绑定一个值,并监听该值的变化。
If it's a DependencyProperty
of a separate class, the easiest way is to bind a value to it, and listen to changes on that value.
如果DP是您在自己的类中实现的DP,那么您可以 DependencyProperty 时,noreferrer>注册PropertyChangedCallback 。您可以使用它来监听属性的更改。
If the DP is one you're implementing in your own class, then you can register a PropertyChangedCallback when you create the DependencyProperty
. You can use this to listen to changes of the property.
如果您正在使用子类,可以使用 OverrideMetadata 将您自己的 PropertyChangedCallback
添加到DP将被调用而不是任何原始的。
If you're working with a subclass, you can use OverrideMetadata to add your own PropertyChangedCallback
to the DP that will get called instead of any original one.
这篇关于听取依赖财产的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!