Exemplo n.º 1
0
        private CommandHandlerWrapper FindCommandHandlerInVisualTree(CommandDefinitionBase commandDefinition, IInputElement target)
        {
            if (!(target is DependencyObject visualObject))
            {
                return(null);
            }

            object previousDataContext = null;

            do
            {
                var frameworkElement = visualObject as FrameworkElement;
                var dataContext      = frameworkElement?.DataContext;
                if (dataContext != null && !ReferenceEquals(dataContext, previousDataContext))
                {
                    if (dataContext is ICommandRerouter context)
                    {
                        var commandRerouter = context;
                        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.º 2
0
        public ICommandHandlerProxy GetCommandHandlerProxy(CommandDefinitionBase commandDefinition)
        {
            var handler = GetCommandHandler(commandDefinition);

            if (handler != null)
            {
                var type = typeof(CommandHandlerProxy <>).MakeGenericType(commandDefinition.GetType());
                return((ICommandHandlerProxy)Activator.CreateInstance(type, handler));
            }

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

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

            var activeItemViewModel = shell.ActiveLayoutItemBase;

            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.º 4
0
 public ICommandHandler GetCommandHandler(CommandDefinitionBase commandDefinition)
 {
     _commandHandlers.TryGetValue(commandDefinition.GetType(), out var handler);
     return(handler);
 }