Silverlight: How to receive notification of a change in an inherited DependencyProperty

前端 未结 5 1566
无人共我
无人共我 2020-12-24 08:53

I have a control which inherits from (you guessed it) Control. I want to receive a notification whenever the FontSize or Style properties are chang

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 09:29

    This is what I always use (haven't tested it on SL though, just on WPF):

        /// 
        /// This method registers a callback on a dependency object to be called
        /// when the value of the DP changes.
        /// 
        /// The owning object.
        /// The DependencyProperty to watch.
        /// The action to call out when the DP changes.
        public static void RegisterDepPropCallback(object owner, DependencyProperty property, EventHandler handler)
        {
            // FIXME: We could implement this as an extension, but let's not get
            // too Ruby-like
            var dpd = DependencyPropertyDescriptor.FromProperty(property, owner.GetType());
            dpd.AddValueChanged(owner, handler);
        }
    

提交回复
热议问题