Condividi tramite


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);
public static void AddPreviewCanExecuteHandler (System.Windows.UIElement element, System.Windows.Input.CanExecuteRoutedEventHandler handler);
static member AddPreviewCanExecuteHandler : System.Windows.UIElement * System.Windows.Input.CanExecuteRoutedEventHandler -> unit
Public Shared Sub AddPreviewCanExecuteHandler (element As UIElement, handler As CanExecuteRoutedEventHandler)

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 Sub HelpCmdExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
    ' OpenHelpFile opens the help file
    OpenHelpFile()
End Sub
private void HelpCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // HelpFilesExists() determines if the help file exists
    if (HelpFileExists() == true)
    {
        e.CanExecute = true;
    }
    else
    {
        e.CanExecute = false;
    }
}
Private Sub HelpCmdCanExecute(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
    ' HelpFilesExists() determines if the help file exists
    If HelpFileExists() = True Then
        e.CanExecute = True
    Else
        e.CanExecute = False
    End If
End Sub

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

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

Si applica a

Vedi anche