CommandManager.AddPreviewCanExecuteHandler Metodo

Definizione

Associa l'oggetto CanExecuteRoutedEventHandler specificato all'elemento specificato.

public static void AddPreviewCanExecuteHandler (System.Windows.UIElement element, System.Windows.Input.CanExecuteRoutedEventHandler handler);

Parametri

element
UIElement

L'elemento a cui associare handler.

handler
CanExecuteRoutedEventHandler

Il gestore dell'evento CanExecute.

Eccezioni

element o handler è null.

Esempio

L'esempio seguente crea un CanExecuteRoutedEventHandler oggetto e ExecutedRoutedEventHandler li associa a un Button oggetto che è un'origine comando per il Help comando.

Prima di tutto, viene Button creato e associato al Help comando.

<Button Command="ApplicationCommands.Help"
        Name="helpButton">Help</Button>

Successivamente, vengono creati i metodi CanExecuteRoutedEventHandler e ExecutedRoutedEventHandler.

private void HelpCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
    // OpenHelpFile opens the help file
    OpenHelpFile();
}
private void HelpCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // HelpFilesExists() determines if the help file exists
    if (HelpFileExists() == true)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}

Infine, i gestori vengono collegati all'uso di ButtonAddCanExecuteHandler e AddExecutedHandler.

CommandManager.AddExecutedHandler(helpButton, HelpCmdExecuted);
CommandManager.AddCanExecuteHandler(helpButton, HelpCmdCanExecute);

Si applica a

Prodotto Versioni
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Vedi anche