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

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

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBlock.FontSizeProperty, typeof(TextBlock));
if (dpd != null)
{
    dpd.AddValueChanged(textBlockTitle, OnFontSizeChanged);
}

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

		public static void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
		{
			//Bind to a depedency property  
			Binding bind = new Binding(propertyName) { Source = element };
			DependencyProperty dp = System.Windows.DependencyProperty.RegisterAttached(
				"ListenAttached" + propertyName,
				typeof(object),
				typeof(UserControl),
				new System.Windows.PropertyMetadata(callback));
			element.SetBinding(dp, bind);
		}

也可以参考这个帖子:

http://blog.thekieners.com/2010/08/27/listening-to-dependencyproperty-changes-in-silverlight/


你可能感兴趣的:(跌跌撞撞的Win8,Metro,style,App开发)