Silverlight:如何在继承的DependencyProperty中接收更改的通知 [英] Silverlight: How to receive notification of a change in an inherited DependencyProperty
问题描述
我有一个控件(该控件继承自您)。
每当更改 FontSize
或 Style
属性时,我都希望收到通知。在WPF中,我可以通过调用 DependencyProperty.OverrideMetadata()
来实现。当然,类似的有用的东西在Silverlight中没有位置。那么,一个人怎么会收到这类通知呢?
I have a control which inherits from (you guessed it) Control.
I want to receive a notification whenever the FontSize
or Style
properties are changed. In WPF, I would do that by calling DependencyProperty.OverrideMetadata()
. Of course, useful things like that have no place in Silverlight. So, how might one receive those kinds of notifications?
推荐答案
我认为这是一种更好的方法。
I think here is a better way. 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);
}
现在,您可以调用RegisterForNotification来注册属性更改通知
And now, you can call RegisterForNotification to register for a change notification of a property of an element, like .
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
使用Silverlight 4.0 Beta。
Using Silverlight 4.0 beta.
这篇关于Silverlight:如何在继承的DependencyProperty中接收更改的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!