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

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

[csharp]  view plain copy
  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 plain copy
  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. }  

你可能感兴趣的:(#,WPF)