CommandManager.AddCanExecuteHandler Método

Definición

Asocia el CanExecuteRoutedEventHandler especificado al 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

Elemento al que se va a asociar handler.

handler
CanExecuteRoutedEventHandler

puede ejecutar el controlador.

Excepciones

element o handler es null.

Ejemplos

En el ejemplo siguiente se crea un CanExecuteRoutedEventHandler y un ExecutedRoutedEventHandler y se adjuntan a un objeto Button que es un origen de comandos para el Help comando.

En primer lugar, Button se crea y se asocia con el Help comando .

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

Después, se crean CanExecuteRoutedEventHandler y 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

Y, por último, los controladores se adjuntan a Button mediante y AddCanExecuteHandlerAddExecutedHandler.

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

Se aplica a

Consulte también