CommandManager.AddCanExecuteHandler 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的 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
為可以執行處理常式。
例外狀況
element
或 handler
為 null
。
範例
下列範例會 CanExecuteRoutedEventHandler 建立 和 ExecutedRoutedEventHandler ,並將其附加至 Button ,這是命令的 Help 命令來源。
首先,會 Button 建立 並與 命令相關聯 Help 。
<Button Command="ApplicationCommands.Help"
Name="helpButton">Help</Button>
接下來,會建立 CanExecuteRoutedEventHandler 和 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
最後,處理程式會使用 AddCanExecuteHandler 和AddExecutedHandler附加至 Button 。
CommandManager.AddExecutedHandler(helpButton, HelpCmdExecuted);
CommandManager.AddCanExecuteHandler(helpButton, HelpCmdCanExecute);
CommandManager.AddExecutedHandler(helpButton, AddressOf HelpCmdExecuted)
CommandManager.AddCanExecuteHandler(helpButton, AddressOf HelpCmdCanExecute)