Exemplo n.º 1
0
        public CommandHandlerWrapper GetCommandHandler(CommandDefinitionBase commandDefinition)
        {
            CommandHandlerWrapper commandHandler;

            var shell = IoC.Get<IShell>();

            var activeItemViewModel = shell.ActiveLayoutItem;
            if (activeItemViewModel != null)
            {
                commandHandler = GetCommandHandlerForLayoutItem(commandDefinition, activeItemViewModel);
                if (commandHandler != null)
                    return commandHandler;
            }

            var activeDocumentViewModel = shell.ActiveItem;
            if (activeDocumentViewModel != null && !Equals(activeDocumentViewModel, activeItemViewModel))
            {
                commandHandler = GetCommandHandlerForLayoutItem(commandDefinition, activeDocumentViewModel);
                if (commandHandler != null)
                    return commandHandler;
            }

            // If none of the objects in the DataContext hierarchy handle the command,
            // fallback to the global handler.
            if (!_globalCommandHandlerWrappers.TryGetValue(commandDefinition.GetType(), out commandHandler))
                return null;

            return commandHandler;
        }
Exemplo n.º 2
0
        public CommandHandlerWrapper GetCommandHandler(CommandDefinitionBase commandDefinition)
        {
            CommandHandlerWrapper commandHandler;

            var activeItemViewModel = IoC.Get<IShell>().ActiveLayoutItem;
            if (activeItemViewModel != null)
            {
                var activeItemView = ViewLocator.LocateForModel(activeItemViewModel, null, null);
                var activeItemWindow = Window.GetWindow(activeItemView);
                if (activeItemWindow != null)
                {
                    var startElement = FocusManager.GetFocusedElement(activeItemWindow);

                    // First, we look at the currently focused element, and iterate up through
                    // the tree, giving each DataContext a chance to handle the command.
                    commandHandler = FindCommandHandlerInVisualTree(commandDefinition, startElement);
                    if (commandHandler != null)
                        return commandHandler;
                }
            }

            // If none of the objects in the DataContext hierarchy handle the command,
            // fallback to the global handler.
            if (!_globalCommandHandlerWrappers.TryGetValue(commandDefinition.GetType(), out commandHandler))
                return null;

            return commandHandler;
        }
Exemplo n.º 3
0
        public CommandHandlerWrapper GetCommandHandler(CommandDefinitionBase commandDefinition)
        {
            CommandHandlerWrapper commandHandler;

            var activeItemViewModel = IoC.Get <IShell>().ActiveLayoutItem;

            if (activeItemViewModel != null)
            {
                var activeItemView   = ViewLocator.LocateForModel(activeItemViewModel, null, null);
                var activeItemWindow = Window.GetWindow(activeItemView);
                if (activeItemWindow != null)
                {
                    var startElement = FocusManager.GetFocusedElement(activeItemWindow);

                    // First, we look at the currently focused element, and iterate up through
                    // the tree, giving each DataContext a chance to handle the command.
                    commandHandler = FindCommandHandlerInVisualTree(commandDefinition, startElement);
                    if (commandHandler != null)
                    {
                        return(commandHandler);
                    }
                }
            }

            // If none of the objects in the DataContext hierarchy handle the command,
            // fallback to the global handler.
            if (!_globalCommandHandlerWrappers.TryGetValue(commandDefinition.GetType(), out commandHandler))
            {
                return(null);
            }

            return(commandHandler);
        }
Exemplo n.º 4
0
        private CommandHandlerWrapper FindCommandHandlerInVisualTree(CommandDefinitionBase commandDefinition, IInputElement target)
        {
            var visualObject = target as DependencyObject;

            if (visualObject == null)
            {
                return(null);
            }

            object previousDataContext = null;

            do
            {
                var frameworkElement = visualObject as FrameworkElement;
                if (frameworkElement != null)
                {
                    var dataContext = frameworkElement.DataContext;
                    if (dataContext != null && !ReferenceEquals(dataContext, previousDataContext))
                    {
                        if (dataContext is ICommandRerouter)
                        {
                            var commandRerouter = (ICommandRerouter)dataContext;
                            var commandTarget   = commandRerouter.GetHandler(commandDefinition);
                            if (commandTarget != null)
                            {
                                if (IsCommandHandlerForCommandDefinitionType(commandTarget, commandDefinition.GetType()))
                                {
                                    return(CreateCommandHandlerWrapper(commandDefinition.GetType(), commandTarget));
                                }
                                throw new InvalidOperationException("This object does not handle the specified command definition.");
                            }
                        }

                        if (IsCommandHandlerForCommandDefinitionType(dataContext, commandDefinition.GetType()))
                        {
                            return(CreateCommandHandlerWrapper(commandDefinition.GetType(), dataContext));
                        }

                        previousDataContext = dataContext;
                    }
                }
                visualObject = VisualTreeHelper.GetParent(visualObject);
            } while (visualObject != null);

            return(null);
        }
Exemplo n.º 5
0
        public CommandHandlerWrapper GetCommandHandler(CommandDefinitionBase commandDefinition)
        {
            CommandHandlerWrapper commandHandler;

            var shell = IoC.Get <IShell>();

            var activeItemViewModel = shell.ActiveLayoutItem;

            if (activeItemViewModel != null)
            {
                commandHandler = GetCommandHandlerForLayoutItem(commandDefinition, activeItemViewModel);
                if (commandHandler != null)
                {
                    return(commandHandler);
                }
            }

            var activeDocumentViewModel = shell.ActiveItem;

            if (activeDocumentViewModel != null && !Equals(activeDocumentViewModel, activeItemViewModel))
            {
                commandHandler = GetCommandHandlerForLayoutItem(commandDefinition, activeDocumentViewModel);
                if (commandHandler != null)
                {
                    return(commandHandler);
                }
            }

            // If none of the objects in the DataContext hierarchy handle the command,
            // fallback to the global handler.
            if (!_globalCommandHandlerWrappers.TryGetValue(commandDefinition.GetType(), out commandHandler))
            {
                return(null);
            }

            return(commandHandler);
        }
Exemplo n.º 6
0
        private CommandHandlerWrapper FindCommandHandlerInVisualTree(CommandDefinitionBase commandDefinition, IInputElement target)
        {
            var visualObject = target as DependencyObject;
            if (visualObject == null)
                return null;

            object previousDataContext = null;
            do
            {
                var frameworkElement = visualObject as FrameworkElement;
                if (frameworkElement != null)
                {
                    var dataContext = frameworkElement.DataContext;
                    if (dataContext != null && !ReferenceEquals(dataContext, previousDataContext))
                    {
                        if (dataContext is ICommandRerouter)
                        {
                            var commandRerouter = (ICommandRerouter) dataContext;
                            var commandTarget = commandRerouter.GetHandler(commandDefinition);
                            if (commandTarget != null)
                            {
                                if (IsCommandHandlerForCommandDefinitionType(commandTarget, commandDefinition.GetType()))
                                    return CreateCommandHandlerWrapper(commandDefinition.GetType(), commandTarget);
                                throw new InvalidOperationException("This object does not handle the specified command definition.");
                            }
                        }

                        if (IsCommandHandlerForCommandDefinitionType(dataContext, commandDefinition.GetType()))
                            return CreateCommandHandlerWrapper(commandDefinition.GetType(), dataContext);

                        previousDataContext = dataContext;
                    }
                }
                visualObject = VisualTreeHelper.GetParent(visualObject);
            } while (visualObject != null);

            return null;
        }