Aracılığıyla paylaş


Nasıl yapılır: Komut Destekli Denetime Komut Bağlama

Aşağıdaki örnek, komutu için yerleşik destek sunan bir Control öğesine nasıl bağlanıldığını RoutedCommand gösterir. Komutları birden çok kaynağa bağlayan eksiksiz bir örnek için Bkz . Özel Yönlendirilmiş Komut Örneği Oluşturma.

Örnek

Windows Presentation Foundation (WPF), uygulama programcıların düzenli olarak karşılaştığı yaygın komutlardan oluşan bir kitaplık sağlar. Komut kitaplığını oluşturan sınıflar şunlardır: ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommandsve EditingCommands.

Bu sınıfları oluşturan statik RoutedCommand nesneler komut mantığı sağlamaz. komutunun mantığı komutuyla CommandBindingilişkilendirilir. Bazı denetimlerde bazı komutlar için CommandBindings yerleşik olarak bulunur. Bu mekanizma, gerçek uygulama değişebilirken komutun semantiğinin aynı kalmasını sağlar. TextBoxÖrneğin, Paste komutu görüntüleri desteklemek için tasarlanmış bir denetimden farklı işler, ancak bir şeyi yapıştırmanın ne anlama geldiğinin temel fikri aynı kalır. Komut mantığı komut tarafından sağlanamaz, ancak denetim veya uygulama tarafından sağlanmalıdır.

WPF'deki birçok denetim, komut kitaplığındaki bazı komutlar için yerleşik desteğe sahiptir. TextBox, örneğin, , , Copy, CutRedove Undogibi Pasteuygulama düzenleme komutlarının çoğunu destekler. Uygulama geliştiricisinin bu denetimlerle çalışmak için bu komutları almak için özel bir şey yapması gerekmez. TextBox komutu yürütülürken komut hedefi ise, denetimin yerleşik öğesini kullanarak CommandBinding komutu işler.

Aşağıda, komutun hedefi olan komutun Paste komut kaynağı olarak a'nın TextBox nasıl kullanılacağı MenuItem gösterilmektedir. yapıştırma işleminin nasıl TextBox gerçekleştirildiğini tanımlayan tüm mantık denetime TextBox yerleşik olarak eklenir.

bir MenuItem oluşturulur ve Command özelliği komutuna Paste ayarlanır. CommandTarget açıkça nesnesine TextBox ayarlanmadı. CommandTarget ayarlanmadığında, komutun hedefi klavye odağı olan öğedir. Klavye odağı olan öğe komutu desteklemiyorsa Paste veya şu anda yapıştırma komutunu yürütemiyorsa (örneğin pano boşsa) MenuItem gri görünür.

<Window x:Class="SDKSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MenuItemCommandTask"
    >
    <DockPanel>
      <Menu DockPanel.Dock="Top">
        <MenuItem Command="ApplicationCommands.Paste" Width="75" />
      </Menu>
      <TextBox BorderBrush="Black" BorderThickness="2" Margin="25"
               TextWrapping="Wrap">
        The MenuItem will not be enabled until
        this TextBox gets keyboard focus  
      </TextBox>
    </DockPanel>
</Window>
// Window1 constructor
public Window1()
{
    InitializeComponent();

    // Instantiating UIElements.
    DockPanel mainPanel = new DockPanel();
    Menu mainMenu = new Menu();
    MenuItem pasteMenuItem = new MenuItem();
    TextBox mainTextBox = new TextBox();

    // Associating the MenuItem with the Paste command.
    pasteMenuItem.Command = ApplicationCommands.Paste;

    // Setting properties on the TextBox.
    mainTextBox.Text =
        "The MenuItem will not be enabled until this TextBox receives keyboard focus.";
    mainTextBox.Margin = new Thickness(25);
    mainTextBox.BorderBrush = Brushes.Black;
    mainTextBox.BorderThickness = new Thickness(2);
    mainTextBox.TextWrapping = TextWrapping.Wrap;

    // Attaching UIElements to the Window.
    this.AddChild(mainPanel);
    mainMenu.Items.Add(pasteMenuItem);
    mainPanel.Children.Add(mainMenu);
    mainPanel.Children.Add(mainTextBox);

    // Defining DockPanel layout.
    DockPanel.SetDock(mainMenu, Dock.Top);
    DockPanel.SetDock(mainTextBox, Dock.Bottom);
}
' Window1 constructor
Public Sub New()
    InitializeComponent()

    ' Instantiating UIElements.
    Dim mainPanel As New DockPanel()
    Dim mainMenu As New Menu()
    Dim pasteMenuItem As New MenuItem()
    Dim mainTextBox As New TextBox()

    ' Associating the MenuItem with the Paste command.
    pasteMenuItem.Command = ApplicationCommands.Paste

    ' Setting properties on the TextBox.
    mainTextBox.Text = "The MenuItem will not be enabled until this TextBox receives keyboard focus."
    mainTextBox.Margin = New Thickness(25)
    mainTextBox.BorderBrush = Brushes.Black
    mainTextBox.BorderThickness = New Thickness(2)
    mainTextBox.TextWrapping = TextWrapping.Wrap

    ' Attaching UIElements to the Window.
    Me.AddChild(mainPanel)
    mainMenu.Items.Add(pasteMenuItem)
    mainPanel.Children.Add(mainMenu)
    mainPanel.Children.Add(mainTextBox)

    ' Defining DockPanel layout.
    DockPanel.SetDock(mainMenu, Dock.Top)
    DockPanel.SetDock(mainTextBox, Dock.Bottom)
End Sub

Ayrıca bkz.