CommandManager.AddPreviewExecutedHandler(UIElement, ExecutedRoutedEventHandler) メソッド

定義

指定した ExecutedRoutedEventHandler を指定した要素に割り当てます。Attaches the specified ExecutedRoutedEventHandler to the specified element.

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

パラメーター

element
UIElement

handler の割り当て先の要素。The element to attach handler to.

handler
ExecutedRoutedEventHandler

実行可能ハンドラー。The can execute handler.

例外

element または handlernull です。element or handler is null.

次の例では、とを作成 CanExecuteRoutedEventHandler ExecutedRoutedEventHandler し、 Button コマンドのコマンドソースであるにアタッチし Help ます。The following example creates a CanExecuteRoutedEventHandler and an ExecutedRoutedEventHandler and attaches them to a Button which is a command source for the Help command.

まず、 Button が作成され、コマンドに関連付けられ Help ます。First, the Button is created and associated with the Help command.

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

次に、CanExecuteRoutedEventHandlerExecutedRoutedEventHandler が作成されます。Next, the CanExecuteRoutedEventHandler and the ExecutedRoutedEventHandler are created.

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

最後に、およびを使用して、ハンドラーがにアタッチされ Button AddCanExecuteHandler AddExecutedHandler ます。Finally, the handlers are attached to the Button using the AddCanExecuteHandler and AddExecutedHandler.

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

適用対象

こちらもご覧ください