监听依赖属性的变化 [英] 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.
推荐答案
如果它是一个单独类的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 是您在自己的类中实现的,那么您可以 在创建 DependencyProperty
时注册一个 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屋!