在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)