次の例では、Windows Presentation Foundation (WPF) でコマンドを使用する方法を示します。 この例では、 RoutedCommand を Buttonに関連付け、 CommandBindingを作成し、 RoutedCommandを実装するイベント ハンドラーを作成する方法を示します。 コマンド実行の詳細については、「コマンド実行の 概要」を参照してください。
例
コードの最初のセクションでは、ButtonとStackPanelで構成されるユーザー インターフェイス (UI) を作成し、コマンド ハンドラーをCommandBindingに関連付けるRoutedCommandを作成します。
CommandのButton プロパティは、Close コマンドに関連付けられています。
CommandBindingは、ルート CommandBindingCollectionのWindowに追加されます。 Executedイベント ハンドラーとCanExecute イベント ハンドラーは、このバインドにアタッチされ、Close コマンドに関連付けられます。
CommandBindingがないと、コマンド ロジックがなく、コマンドを呼び出すメカニズムのみが存在します。 Buttonがクリックされると、コマンド ターゲットでPreviewExecutedRoutedEventが発生し、その後にExecutedRoutedEventが発生します。 これらのイベントは、その特定のコマンドの CommandBinding を探して要素ツリーを走査します。 RoutedEventトンネルとバブルが要素ツリーを通過するため、CommandBindingが配置される場所に注意する必要があります。 CommandBindingがコマンド ターゲットの兄弟上にある場合、またはRoutedEventのルートにない別のノードにある場合、CommandBindingはアクセスされません。
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CloseCommand"
Name="RootWindow"
>
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"
CanExecute="CanExecuteHandler"
/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close File" />
</StackPanel>
</Window>
// Create ui elements.
StackPanel CloseCmdStackPanel = new StackPanel();
Button CloseCmdButton = new Button();
CloseCmdStackPanel.Children.Add(CloseCmdButton);
// Set Button's properties.
CloseCmdButton.Content = "Close File";
CloseCmdButton.Command = ApplicationCommands.Close;
// Create the CommandBinding.
CommandBinding CloseCommandBinding = new CommandBinding(
ApplicationCommands.Close, CloseCommandHandler, CanExecuteHandler);
// Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding);
' Create ui elements.
Dim CloseCmdStackPanel As New StackPanel()
Dim CloseCmdButton As New Button()
CloseCmdStackPanel.Children.Add(CloseCmdButton)
' Set Button's properties.
CloseCmdButton.Content = "Close File"
CloseCmdButton.Command = ApplicationCommands.Close
' Create the CommandBinding.
Dim CloseCommandBinding As New CommandBinding(ApplicationCommands.Close, AddressOf CloseCommandHandler, AddressOf CanExecuteHandler)
' Add the CommandBinding to the root Window.
RootWindow.CommandBindings.Add(CloseCommandBinding)
コードの次のセクションでは、 Executed と CanExecute イベント ハンドラーを実装します。
Executed ハンドラーは、開いているファイルを閉じるメソッドを呼び出します。
CanExecute ハンドラーは、ファイルが開いているかどうかを判断するメソッドを呼び出します。 ファイルが開いている場合、 CanExecute は true
に設定され、それ以外の場合は false
に設定されます。
// Executed event handler.
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
// Calls a method to close the file and release resources.
CloseFile();
}
// CanExecute event handler.
private void CanExecuteHandler(object sender, CanExecuteRoutedEventArgs e)
{
// Call a method to determine if there is a file open.
// If there is a file open, then set CanExecute to true.
if (IsFileOpened())
{
e.CanExecute = true;
}
// if there is not a file open, then set CanExecute to false.
else
{
e.CanExecute = false;
}
}
' Executed event handler.
Private Sub CloseCommandHandler(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
' Calls a method to close the file and release resources.
CloseFile()
End Sub
' CanExecute event handler.
Private Sub CanExecuteHandler(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
' Call a method to determine if there is a file open.
' If there is a file open, then set CanExecute to true.
If IsFileOpened() Then
e.CanExecute = True
' if there is not a file open, then set CanExecute to false.
Else
e.CanExecute = False
End If
End Sub
こちらも参照ください
.NET Desktop feedback