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 测试版.
Using Silverlight 4.0 beta.
这篇关于Silverlight:如何接收继承的 DependencyProperty 中更改的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!