| File: Microsoft\Windows\Input\CommandHelpers.cs | Web Access |
| Project: src\wpf\src\Microsoft.DotNet.Wpf\src\System.Windows.Controls.Ribbon\System.Windows.Controls.Ribbon.csproj (System.Windows.Controls.Ribbon) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Windows; using System.Windows.Input; namespace Microsoft.Windows.Input { /// <summary> /// A helper class for executing Commands. /// </summary> internal static class CommandHelpers { /// <summary> /// Carries out the specified action for the CommandSource's command as a RoutedCommand if possible, /// otherwise as a regular Command. /// </summary> /// <param name="parameter">The parameter to the ICommandSource.</param> /// <param name="commandSource">The ICommandSource being executed.</param> internal static void InvokeCommandSource(object parameter, object previewParameter, ICommandSource commandSource, CommandOperation operation) { ICommand command = commandSource.Command; if (command != null) { RoutedCommand routed = command as RoutedCommand; if (routed != null) { IInputElement target = commandSource.CommandTarget; if (target == null) { target = commandSource as IInputElement; } if (routed.CanExecute(parameter, target)) { //Debug.Assert(operation == CommandOperation.Execute, "We do not support Previewing RoutedCommands."); switch (operation) { case CommandOperation.Execute: routed.Execute(parameter, target); break; } } } else if (command.CanExecute(parameter)) { IPreviewCommand previewCommand; switch (operation) { case CommandOperation.Preview: previewCommand = command as IPreviewCommand; previewCommand?.Preview(previewParameter); break; case CommandOperation.CancelPreview: previewCommand = command as IPreviewCommand; previewCommand?.CancelPreview(); break; case CommandOperation.Execute: command.Execute(parameter); break; } } } } /// <summary> /// Queries CanExecute status for the CommandSource's command as a RoutedCommand if possible, /// otherwise as a regular Command. /// </summary> /// <param name="parameter">The parameter to the ICommandSource.</param> /// <param name="commandSource">The ICommandSource being executed.</param> internal static bool CanExecuteCommandSource(object parameter, ICommandSource commandSource) { ICommand command = commandSource.Command; if (command != null) { RoutedCommand routed = command as RoutedCommand; if (routed != null) { IInputElement target = commandSource.CommandTarget; if (target == null) { target = commandSource as IInputElement; } return routed.CanExecute(parameter, target); } else { return command.CanExecute(parameter); } } return true; } } internal enum CommandOperation { Preview, CancelPreview, Execute } }