In a Silverlight application I'm trying to find out when a property on a usercontrol has changed. I'm interested in one particular DependencyProperty, but unfortunately the control itself doesn't implement INotifyPropertyChanged.
在Silverlight应用程序中,我试图找出usercontrol上的属性何时发生了变化。我对一个特定的DependencyProperty感兴趣,但不幸的是控件本身并没有实现INotifyPropertyChanged。
Is there any other way of determining if the value has changed?
有没有其他方法可以确定价值是否已经改变?
4 个解决方案
#1
2
In WPF you have DependencyPropertyDescriptor.AddValueChanged, but unfortunately in Silverlight there's no such thing. So the answer is no.
在WPF中你有DependencyPropertyDescriptor.AddValueChanged,但不幸的是在Silverlight中没有这样的东西。所以答案是否定的。
Maybe if you explain what are you trying to do you can workaround the situation, or use bindings.
也许如果你解释你想要做什么,你可以解决这种情况,或使用绑定。
#2
5
You can. Atleast I did. Still need to see the pros and Cons.
您可以。至少我做到了。仍然需要看到利弊。
/// 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);
}
And now, you can call RegisterForNotification to register for a change notification of a property of an element, like .
现在,您可以调用RegisterForNotification来注册元素属性的更改通知,例如。
RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
See my post here on the same http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html
在http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html上查看我的帖子。
#3
1
As Jon Galloway posted on another thread, you might be able to use something like WeakReference to wrap properties you're interested in and re-register them in your own class. This is WPF code but the concept doesn't rely on DependencyPropertyDescriptor.
正如Jon Galloway在另一个帖子上发布的那样,你可以使用像WeakReference这样的东西来包装你感兴趣的属性,并在你自己的类中重新注册它们。这是WPF代码,但该概念不依赖于DependencyPropertyDescriptor。
#4
0
Check out the following link. It showns how to get around the problem in silverlight where you don't have DependencyPropertyDescriptor.AddValueChanged
查看以下链接。它展示了如何解决Silverlight中没有DependencyPropertyDescriptor.AddValueChanged的问题
http://themechanicalbride.blogspot.com/2008/10/building-observable-model-in.html
#1
2
In WPF you have DependencyPropertyDescriptor.AddValueChanged, but unfortunately in Silverlight there's no such thing. So the answer is no.
在WPF中你有DependencyPropertyDescriptor.AddValueChanged,但不幸的是在Silverlight中没有这样的东西。所以答案是否定的。
Maybe if you explain what are you trying to do you can workaround the situation, or use bindings.
也许如果你解释你想要做什么,你可以解决这种情况,或使用绑定。
#2
5
You can. Atleast I did. Still need to see the pros and Cons.
您可以。至少我做到了。仍然需要看到利弊。
/// 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);
}
And now, you can call RegisterForNotification to register for a change notification of a property of an element, like .
现在,您可以调用RegisterForNotification来注册元素属性的更改通知,例如。
RegisterForNotification("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
RegisterForNotification("Value", this.sliderMain, (d, e) => MessageBox.Show("Value changed"));
See my post here on the same http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html
在http://amazedsaint.blogspot.com/2009/12/silverlight-listening-to-dependency.html上查看我的帖子。
#3
1
As Jon Galloway posted on another thread, you might be able to use something like WeakReference to wrap properties you're interested in and re-register them in your own class. This is WPF code but the concept doesn't rely on DependencyPropertyDescriptor.
正如Jon Galloway在另一个帖子上发布的那样,你可以使用像WeakReference这样的东西来包装你感兴趣的属性,并在你自己的类中重新注册它们。这是WPF代码,但该概念不依赖于DependencyPropertyDescriptor。
#4
0
Check out the following link. It showns how to get around the problem in silverlight where you don't have DependencyPropertyDescriptor.AddValueChanged
查看以下链接。它展示了如何解决Silverlight中没有DependencyPropertyDescriptor.AddValueChanged的问题
http://themechanicalbride.blogspot.com/2008/10/building-observable-model-in.html