共用方式為


CommandManager.AddExecutedHandler 方法

定義

將指定的 ExecutedRoutedEventHandler 附加至指定的項目。

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

參數

element
UIElement

handler 要附加的目標項目。

handler
ExecutedRoutedEventHandler

已執行的處理常式。

例外狀況

elementhandlernull

範例

下列範例會 CanExecuteRoutedEventHandler 建立 和 ExecutedRoutedEventHandler ,並將其附加至 Button ,這是命令的 Help 命令來源。

首先,會 Button 建立 並與 命令相關聯 Help

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

接下來,會建立 CanExecuteRoutedEventHandlerExecutedRoutedEventHandler

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

最後,處理程式會使用 AddCanExecuteHandlerAddExecutedHandler附加至 Button

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

適用於

另請參閱