Пример #1
0
        /// <summary>
        ///     Re-raises an event with as a different RoutedEvent.
        /// </summary>
        /// <remarks>
        ///     Only used internally.  Added to support cracking generic MouseButtonDown/Up events
        ///     into MouseLeft/RightButtonDown/Up events.
        /// </remarks>
        /// <param name="args">
        ///     RoutedEventsArgs to re-raise with a new RoutedEvent
        /// </param>
        /// <param name="newEvent">
        ///     The new RoutedEvent to be associated with the RoutedEventArgs
        /// </param>
        private static void ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
        {
            // Preseve and change the RoutedEvent
            RoutedEvent preservedRoutedEvent = args.RoutedEvent;
            args.OverrideRoutedEvent( newEvent );

            // Preserve Source
            object preservedSource = args.Source;

            EventRoute route = EventRouteFactory.FetchObject(args.RoutedEvent);

            if( TraceRoutedEvent.IsEnabled )
            {
                TraceRoutedEvent.Trace(
                    TraceEventType.Start,
                    TraceRoutedEvent.ReRaiseEventAs,
                    args.RoutedEvent,
                    sender,
                    args,
                    args.Handled );
            }

            try
            {
                // Build the route and invoke the handlers
                UIElement.BuildRouteHelper(sender, route, args);

                route.ReInvokeHandlers(sender, args);

                // Restore Source
                args.OverrideSource(preservedSource);

                // Restore RoutedEvent
                args.OverrideRoutedEvent(preservedRoutedEvent);

            }

            finally
            {
                if( TraceRoutedEvent.IsEnabled )
                {
                    TraceRoutedEvent.Trace(
                        TraceEventType.Stop,
                        TraceRoutedEvent.ReRaiseEventAs,
                        args.RoutedEvent,
                        sender,
                        args,
                        args.Handled );
                }
            }

            // Recycle the route object
            EventRouteFactory.RecycleObject(route);
        }