.net 依赖项属性发生更改时是否有通知机制?

.net 依赖项属性发生更改时是否有通知机制?,.net,silverlight,silverlight-2.0,dependency-properties,.net,Silverlight,Silverlight 2.0,Dependency Properties,在Silverlight应用程序中,我试图找出usercontrol上的属性何时发生了更改。我对一个特定的DependencyProperty感兴趣,但不幸的是控件本身没有实现INotifyPropertyChanged 是否有其他方法确定值是否已更改?在WPF中,您已经更改了,但不幸的是,在Silverlight中没有这样的方法。所以答案是否定的 也许如果你解释一下你想做什么,你可以解决这个问题,或者使用绑定。正如Jon Galloway在另一个线程上发布的那样,你可以使用类似WeakRefe

在Silverlight应用程序中,我试图找出usercontrol上的属性何时发生了更改。我对一个特定的DependencyProperty感兴趣,但不幸的是控件本身没有实现INotifyPropertyChanged

是否有其他方法确定值是否已更改?

在WPF中,您已经更改了,但不幸的是,在Silverlight中没有这样的方法。所以答案是否定的


也许如果你解释一下你想做什么,你可以解决这个问题,或者使用绑定。

正如Jon Galloway在另一个线程上发布的那样,你可以使用类似WeakReference的东西来包装你感兴趣的属性,并在你自己的类中重新注册它们。这是WPF代码,但概念不依赖DependencyPropertyDescriptor


查看以下链接。它展示了如何在silverlight中解决这个问题,而您没有DependencyPropertyDescriptor.AddValueChanged


你可以。至少我做到了。仍然需要看到利弊

 /// 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"));

在同一个网站上看到我的帖子

你就是那个人!在我找到你的答案之前,我已经为此挣扎了好几个小时。这会导致垃圾收集出现问题吗?
元素的寿命是否会延长?