共用方式為


CommandManager.AddCanExecuteHandler 方法

定義

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

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)

參數

element
UIElement

handler 要附加的目標項目。

handler
CanExecuteRoutedEventHandler

為可以執行處理常式。

例外狀況

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)

適用於

另請參閱