wp8和wpf里监听控件自带属性的变化

来源:互联网 发布:搞笑一家人大结局知乎 编辑:程序博客网 时间:2024/05/11 16:10

在wpf里,有DependencyPropertyDescriptor类可以轻松实现监听控件自带属性的变化,如下:

[csharp] view plaincopy
  1. DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBlock.FontSizeProperty, typeof(TextBlock));  
  2. if (dpd != null)  
  3. {  
  4.     dpd.AddValueChanged(textBlockTitle, OnFontSizeChanged);  
  5. }  

但是wp8里没有这个类,我们使用DependencyProperty.RegisterAttached来间接实现:

[csharp] view plaincopy
  1. public static void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)  
  2. {  
  3.     //Bind to a depedency property    
  4.     Binding bind = new Binding(propertyName) { Source = element };  
  5.     DependencyProperty dp = System.Windows.DependencyProperty.RegisterAttached(  
  6.         "ListenAttached" + propertyName,  
  7.         typeof(object),  
  8.         typeof(UserControl),  
  9.         new System.Windows.PropertyMetadata(callback));  
  10.     element.SetBinding(dp, bind);  
  11. }  
0 0
原创粉丝点击