/// <summary>
        /// Attaches an event to the first Parent of obj (which must be a 
        /// FrameworkElement) which declares a public method with the name 
        /// "handler" and arguments of types "types".
        /// 
        /// When that method is found, attacher is invoked with:
        /// - sender is "obj"
        /// - target is the Parent that declares the method
        /// - method is the method of Parent
        /// </summary>
        /// <param name="obj">the object to which to attach an event</param>
        /// <param name="handler">the name of the method to be found in some Parent of obj</param>
        /// <param name="attacher">indicates how to attach an event to the target</param>
        public static void AttachEventHandler(DependencyObject obj, string methodName, Type[] handlerTypes, EventAttacher attacher)
        {
            FrameworkElement element = obj as FrameworkElement;
            if (element == null)
                throw new ArgumentException("Can only attach events to FrameworkElement instances, not to '" + obj.GetType() + "'");

            element.Loaded += (sender, e) =>
            {
                DependencyObject parent = sender as DependencyObject;

                MethodInfo info = null;
                while (info == null)
                {
                    info = parent.GetType().GetMethod(methodName, handlerTypes);
                    if (info != null)
                    {
                        attacher(sender, parent, info);
                        return;
                    }

                    parent = (parent is FrameworkElement) ? ((FrameworkElement)parent).Parent : null;

                    if (parent == null)
                        throw new ArgumentException("Can't find handler '" + methodName + "' (maybe it's not public, or set in a ControlTemplate?)");
                }
            };
        }
Пример #2
0
        /// <summary>
        /// Attaches an event to the first Parent of obj (which must be a 
        /// FrameworkElement) which declares a public method with the name 
        /// "handler" and arguments of types "types".
        /// 
        /// When that method is found, attacher is invoked with:
        /// - sender is "obj"
        /// - target is the Parent that declares the method
        /// - method is the method of Parent
        /// </summary>
        /// <param name="obj">the object to which to attach an event</param>
        /// <param name="handler">the name of the method to be found in some Parent of obj</param>
        /// <param name="attacher">indicates how to attach an event to the target</param>
        public static void AttachEvent(DependencyObject obj, string handler, Type[] types, EventAttacher attacher)
        {
            FrameworkElement fe = obj as FrameworkElement;
            if (fe == null)
            {
                throw new ArgumentException("Can only attach events to FrameworkElement instances, not to '" + obj.GetType() + "'");
            }

            fe.Loaded += (sender, e) =>
            {
                DependencyObject parent = sender as DependencyObject;

                MethodInfo info = null;
                while (info == null)
                {
                    info = parent.GetType().GetMethod(handler, types);
                    if (info != null)
                    {
                        attacher(sender, parent, info);
                        return;
                    }

                    if (parent is FrameworkElement)
                    {
                        parent = (parent as FrameworkElement).Parent;
                    }
                    else
                    {
                        parent = null;
                    }

                    if (parent == null)
                    {
                        throw new ArgumentException("Can't find handler '" + handler + "' (maybe it's not public?)");
                    }
                }
            };
        }
Пример #3
0
 protected override void SubRevoke(IEntity tVar)
 {
     this.eaAttacher = null;
 }
Пример #4
0
 public EventAttachService(EventAttacher ea)
 {
     this.eaAttacher = ea;
 }
Пример #5
0
        /// <summary>
        /// Attaches an event to the first Parent of obj (which must be a
        /// FrameworkElement) which declares a public method with the name
        /// "handler" and arguments of types "types".
        ///
        /// When that method is found, attacher is invoked with:
        /// - sender is "obj"
        /// - target is the Parent that declares the method
        /// - method is the method of Parent
        /// </summary>
        /// <param name="obj">the object to which to attach an event</param>
        /// <param name="handler">the name of the method to be found in some Parent of obj</param>
        /// <param name="attacher">indicates how to attach an event to the target</param>
        public static void AttachEvent(DependencyObject obj, string handler, Type[] types, EventAttacher attacher)
        {
            FrameworkElement fe = obj as FrameworkElement;

            if (fe == null)
            {
                throw new ArgumentException("Can only attach events to FrameworkElement instances, not to '" + obj.GetType() + "'");
            }

            fe.Loaded += (sender, e) =>
            {
                DependencyObject parent = sender as DependencyObject;

                MethodInfo info = null;
                while (info == null)
                {
                    info = parent.GetType().GetMethod(handler, types);
                    if (info != null)
                    {
                        attacher(sender, parent, info);
                        return;
                    }

                    if (parent is FrameworkElement)
                    {
                        parent = (parent as FrameworkElement).Parent;
                    }
                    else
                    {
                        parent = null;
                    }

                    if (parent == null)
                    {
                        throw new ArgumentException("Can't find handler '" + handler + "' (maybe it's not public?)");
                    }
                }
            };
        }