/// <summary> /// Called when the command binding is executed. /// </summary> internal void HandleExecuted(DependencyObject element, ICommand command, Object parameter, ExecutedRoutedEventData data, Boolean preview) { if (data.Handled) { return; } var handler = preview ? PreviewExecuted : Executed; if (handler != null) { var canExecuteData = CanExecuteRoutedEventData.Retrieve(data.Source as DependencyObject, autorelease: false); try { HandleCanExecute(element, command, parameter, canExecuteData, false); if (canExecuteData.CanExecute) { handler(element, command, parameter, data); data.Handled = true; } } finally { canExecuteData.Release(); } } }
/// <summary> /// Determines whether the command can be executed. /// </summary> /// <param name="view">The view within which the command is being executed.</param> /// <param name="parameter">The command parameter, or <see langword="null"/> if the command /// does not require a parameter.</param> /// <param name="valparameter">The command value parameter, or <see langword="null"/> if the command /// does not require a value parameter. This parameter is intended for use in internal micro-optimizations and /// will appear as one of the fields in the routed event data.</param> /// <param name="target">The element within <paramref name="view"/> at which to begin /// searching for command handlers.</param> /// <param name="continue">A value indicating whether command routing should continue.</param> /// <returns><see langword="true"/> if the command can be executed; otherwise, <see langword="false"/>.</returns> public Boolean CanExecute(PresentationFoundationView view, Object parameter, PrimitiveUnion?valparameter, IInputElement target, out Boolean @continue) { Contract.Require(view, nameof(view)); var uiElement = target as UIElement; if (uiElement != null && uiElement.View != view) { throw new ArgumentException(PresentationStrings.ElementDoesNotBelongToView); } if (uiElement != null) { var data = CanExecuteRoutedEventData.Retrieve(this, valparameter, autorelease: false); try { var evtPreview = EventManager.GetInvocationDelegate <UpfCanExecuteRoutedEventHandler>(CommandManager.PreviewCanExecuteEvent); evtPreview(uiElement, this, parameter, data); if (!data.Handled) { var evt = EventManager.GetInvocationDelegate <UpfCanExecuteRoutedEventHandler>(CommandManager.CanExecuteEvent); evt(uiElement, this, parameter, data); } @continue = data.ContinueRouting; return(data.CanExecute); } finally { data.Release(); } } else { @continue = false; return(false); } }
/// <summary> /// Raises the <see cref="CanExecute"/> event. /// </summary> private void RaiseCanExecute(DependencyObject element, ICommand command, Object parameters, CanExecuteRoutedEventData data) => CanExecute?.Invoke(element, command, parameters, data);
/// <summary> /// Called when the command binding is being queried to determine whether it can execute. /// </summary> internal void HandleCanExecute(DependencyObject element, ICommand command, Object parameter, CanExecuteRoutedEventData data, Boolean preview) { if (data.Handled) { return; } if (preview) { if (PreviewCanExecute != null) { PreviewCanExecute(element, command, parameter, data); if (data.CanExecute) { data.Handled = true; } } } else { if (CanExecute != null) { CanExecute(element, command, parameter, data); if (data.CanExecute) { data.Handled = true; } } else { if (!data.CanExecute && Executed != null) { data.CanExecute = true; data.Handled = true; } } } }