public Command(CommandDefinitionBase commandDefinition) { _commandDefinition = commandDefinition; Text = commandDefinition.Text; ToolTip = commandDefinition.ToolTip; IconSource = commandDefinition.IconSource; }
public Command GetCommand(CommandDefinitionBase commandDefinition) { Command command; if (!_commands.TryGetValue(commandDefinition, out command)) command = _commands[commandDefinition] = new Command(commandDefinition); return command; }
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; }
public KeyGesture GetPrimaryKeyGesture(CommandDefinitionBase commandDefinition) { var keyboardShortcut = _keyboardShortcuts.FirstOrDefault(x => x.CommandDefinition == commandDefinition); return (keyboardShortcut != null) ? keyboardShortcut.KeyGesture : null; }
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; }
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); }
public KeyGesture GetPrimaryKeyGesture(CommandDefinitionBase commandDefinition) { var keyboardShortcut = _keyboardShortcuts.FirstOrDefault(x => x.CommandDefinition == commandDefinition); return((keyboardShortcut != null) ? keyboardShortcut.KeyGesture : null); }
public Command GetCommand(CommandDefinitionBase commandDefinition) { Command command; if (!_commands.TryGetValue(commandDefinition, out command)) { command = _commands[commandDefinition] = new Command(commandDefinition); } return(command); }
object ICommandRerouter.GetHandler(CommandDefinitionBase commandDefinition) { if (commandDefinition is UndoCommandDefinition || commandDefinition is RedoCommandDefinition) { return _shell.ActiveItem; } return null; }
private CommandHandlerWrapper GetCommandHandlerForLayoutItem(CommandDefinitionBase commandDefinition, object activeItemViewModel) { var activeItemView = ViewLocator.LocateForModel(activeItemViewModel, null, null); var activeItemWindow = Window.GetWindow(activeItemView); if (activeItemWindow == null) { return(null); } var startElement = FocusManager.GetFocusedElement(activeItemView) ?? activeItemView; // First, we look at the currently focused element, and iterate up through // the tree, giving each DataContext a chance to handle the command. return(FindCommandHandlerInVisualTree(commandDefinition, startElement)); }
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); }
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); }
private CommandHandlerWrapper GetCommandHandlerForLayoutItem(CommandDefinitionBase commandDefinition, object activeItemViewModel) { var activeItemView = ViewLocator.LocateForModel(activeItemViewModel, null, null); var activeItemWindow = Window.GetWindow(activeItemView); if (activeItemWindow == null) return null; var startElement = FocusManager.GetFocusedElement(activeItemView) ?? activeItemView; // First, we look at the currently focused element, and iterate up through // the tree, giving each DataContext a chance to handle the command. return FindCommandHandlerInVisualTree(commandDefinition, startElement); }
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; }