在 Silverlight 应用程序中,我试图找出用户控件上的属性何时更改。我对一个特定的 DependencyProperty 很感兴趣,但不幸的是控件本身没有实现 INotifyPropertyChanged。
是否有任何其他方法可以确定值是否已更改?
最佳答案
可以。至少我做到了。仍然需要查看优缺点。
/// Listen for change of the dependency property
public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
{
//Bind to a depedency property
Binding b = new Binding(propertyName) { Source = element };
var prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached"+propertyName,
typeof(object),
typeof(UserControl),
new System.Windows.PropertyMetadata(callback));
element.SetBinding(prop, b);
}
现在,您可以调用 RegisterForNotification 来注册元素属性的更改通知,例如 。
RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
在同一 http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html 上查看我的帖子
关于.net - 当依赖属性发生变化时是否有通知机制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/240156/