CommandManager.AddCanExecuteHandler Método

Definição

Anexa o especificado CanExecuteRoutedEventHandler ao elemento especificado.

public:
 static void AddCanExecuteHandler(System::Windows::UIElement ^ element, System::Windows::Input::CanExecuteRoutedEventHandler ^ handler);
public static void AddCanExecuteHandler(System.Windows.UIElement element, System.Windows.Input.CanExecuteRoutedEventHandler handler);
static member AddCanExecuteHandler : System.Windows.UIElement * System.Windows.Input.CanExecuteRoutedEventHandler -> unit
Public Shared Sub AddCanExecuteHandler (element As UIElement, handler As CanExecuteRoutedEventHandler)

Parâmetros

element
UIElement

O elemento a que se deve prender handler .

handler
CanExecuteRoutedEventHandler

O handler pode executar.

Exceções

element ou handler é null.

Exemplos

O exemplo seguinte cria um CanExecuteRoutedEventHandler e um ExecutedRoutedEventHandler e associa-os a Button um que é uma fonte de comando para o Help comando.

Primeiro, o Button é criado e associado ao Help comando.

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

De seguida, os CanExecuteRoutedEventHandler e ExecutedRoutedEventHandler os são criados.

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())
    {
        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

E finalmente, os manipuladores são ligados ao Button usando e AddCanExecuteHandlerAddExecutedHandler.

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

Aplica-se a

Ver também