private static void OnEventNameChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            EventTriggerBehavior behavior = (EventTriggerBehavior)dependencyObject;

            if (behavior.AssociatedObject == null || behavior._resolvedSource == null)
            {
                return;
            }

            string oldEventName = (string)args.OldValue;
            string newEventName = (string)args.NewValue;

            behavior.UnregisterEvent(oldEventName);
            behavior.RegisterEvent(newEventName);
        }
        private void RegisterEvent(string eventName)
        {
            if (string.IsNullOrEmpty(eventName))
            {
                return;
            }

            if (eventName != "Loaded")
            {
                Type      sourceObjectType = _resolvedSource.GetType();
                EventInfo info             = sourceObjectType.GetRuntimeEvent(eventName);
                if (info == null)
                {
                    return;
                }

                MethodInfo methodInfo = typeof(EventTriggerBehavior).GetTypeInfo().GetDeclaredMethod("OnEvent");
                _eventHandler = methodInfo.CreateDelegate(info.EventHandlerType, this);

                _isWindowsRuntimeEvent = EventTriggerBehavior.IsWindowsRuntimeEvent(info);
                if (_isWindowsRuntimeEvent)
                {
                    _addEventHandlerMethod    = add => (EventRegistrationToken)info.AddMethod.Invoke(_resolvedSource, new object[] { add });
                    _removeEventHandlerMethod = token => info.RemoveMethod.Invoke(_resolvedSource, new object[] { token });

                    WindowsRuntimeMarshal.AddEventHandler(_addEventHandlerMethod, _removeEventHandlerMethod, _eventHandler);
                }
                else
                {
                    info.AddEventHandler(_resolvedSource, _eventHandler);
                }
            }
            else if (!_isLoadedEventRegistered)
            {
                FrameworkElement element = _resolvedSource as FrameworkElement;
                if (element != null && !EventTriggerBehavior.IsElementLoaded(element))
                {
                    _isLoadedEventRegistered = true;
                    element.Loaded          += OnEvent;
                }
            }
        }
 private static bool IsWindowsRuntimeEvent(EventInfo eventInfo)
 {
     return(eventInfo != null &&
            EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.EventHandlerType) &&
            EventTriggerBehavior.IsWindowsRuntimeType(eventInfo.DeclaringType));
 }
        private static void OnSourceObjectChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
        {
            EventTriggerBehavior behavior = (EventTriggerBehavior)dependencyObject;

            behavior.SetResolvedSource(behavior.ComputeResolvedSource());
        }