Nasıl yapılır: Komutu Etkinleştirme

Aşağıdaki örnek ' de komutlama kullanımını gösterir Windows Presentation Foundation (WPF) . Örnek, ' a nasıl ilişkilendirileceğini RoutedCommand Button , oluşturma CommandBinding ve öğesini uygulayan olay işleyicilerini oluşturmayı gösterir RoutedCommand . Komutlama hakkında daha fazla bilgi için bkz. komut bilgisine genel bakış.

Örnek

Kodun ilk bölümü, kullanıcı arabirimi (UI) ve ' den oluşan öğesini oluşturur Button StackPanel ve CommandBinding komut işleyicilerini ile ilişkilendiren bir oluşturur RoutedCommand .

CommandÖğesinin özelliği, Button Close komutuyla ilişkilidir.

, CommandBinding Kökün öğesine eklenir CommandBindingCollection Window . ExecutedVe CanExecute olay işleyicileri bu bağlamaya iliştirilir ve Close komutla ilişkilendirilir.

CommandBindingHiçbir komut mantığı olmadığında, yalnızca komutu çağırmak için bir mekanizma. Tıklandığında, Button PreviewExecuted RoutedEvent komut hedefi üzerinde tetiklenir ve sonrasında Executed RoutedEvent . Bu olaylar, belirli bir komut için öğesine bakarak öğe ağacında çapraz geçiş yapar CommandBinding . RoutedEventÖğe ağacı aracılığıyla tünel ve kabarcığun bulunacağı yerde ele alınması gerektiğini belirten bir değer CommandBinding . , CommandBinding Komut hedefinin veya yönlendirmekte olmayan başka bir düğümün eşdüzey kullanılıyorsa, RoutedEvent CommandBinding erişilmez.

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

Kodun sonraki bölümü Executed ve CanExecute olay işleyicilerini uygular.

Executedİşleyici, Açık dosyayı kapatmak için bir yöntem çağırır. CanExecuteİşleyici bir dosyanın açık olup olmadığını belirleme yöntemini çağırır. Bir dosya açıksa, olarak ayarlanır CanExecute true ; Aksi takdirde, olarak ayarlanır 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

Ayrıca bkz.