Bagikan melalui


Cara: Mengaktifkan Perintah

Contoh berikut menunjukkan cara menggunakan perintah di Windows Presentation Foundation (WPF). Contoh menunjukkan cara mengaitkan RoutedCommand ke Button, membuat CommandBinding, dan membuat penanganan aktivitas yang mengimplementasikan RoutedCommand. Untuk informasi selengkapnya tentang perintah, lihat Gambaran Umum Perintah.

Contoh

Bagian pertama kode membuat antarmuka pengguna (UI), yang terdiri dari Button dan StackPanel, dan membuat CommandBinding yang mengaitkan handler perintah dengan RoutedCommand.

Properti Command dari Button dikaitkan dengan Close perintah .

CommandBinding ditambahkan ke CommandBindingCollection akar Window. Penanganan Executed aktivitas dan CanExecute dilampirkan ke pengikatan ini dan terkait dengan Close perintah .

CommandBinding Tanpa tidak ada logika perintah, hanya mekanisme untuk memanggil perintah. Ketika diklik Button , PreviewExecuted RoutedEvent dinaikkan pada target perintah diikuti oleh Executed RoutedEvent. Peristiwa ini melintasi pohon elemen yang mencari CommandBinding perintah tertentu. Perlu dicatat bahwa karena RoutedEvent terowongan dan gelembung melalui pohon elemen, perawatan harus diambil di mana CommandBinding dimasukkan. CommandBinding Jika berada pada saudara kandung target perintah atau simpul lain yang tidak berada di rute RoutedEvent, CommandBinding tidak akan diakses.

<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)

Bagian kode berikutnya mengimplementasikan Executed penanganan aktivitas dan CanExecute .

Handler Executed memanggil metode untuk menutup file yang terbuka. Handler CanExecute memanggil metode untuk menentukan apakah file terbuka. Jika file terbuka, CanExecute diatur ke true; jika tidak, file diatur ke 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

Lihat juga