共用方式為


CommandManager.AddPreviewCanExecuteHandler 方法

定義

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

public:
 static void AddPreviewCanExecuteHandler(System::Windows::UIElement ^ element, System::Windows::Input::CanExecuteRoutedEventHandler ^ handler);
public static void AddPreviewCanExecuteHandler (System.Windows.UIElement element, System.Windows.Input.CanExecuteRoutedEventHandler handler);
static member AddPreviewCanExecuteHandler : System.Windows.UIElement * System.Windows.Input.CanExecuteRoutedEventHandler -> unit
Public Shared Sub AddPreviewCanExecuteHandler (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)

適用於

另請參閱