Nasıl yapılır: Komut Destekli Denetime Komut Bağlama
Aşağıdaki örnek, RoutedCommandControl komutu için yerleşik desteği olan bir öğesine nasıl bağlanacağını göstermektedir. Birden çok kaynağa komutları bağlayan bir örnek için, bkz. özel RoutedCommand örnek örneği oluşturma .
Örnek
Windows Presentation Foundation (WPF), uygulama programcılarının düzenli olarak karşılaştığı yaygın komutlar içeren bir kitaplık sağlar. Komut kitaplığını oluşturan sınıflar şunlardır: ApplicationCommands , ComponentCommands ,, NavigationCommandsMediaCommands ve EditingCommands .
RoutedCommandBu sınıfları oluşturan statik nesneler komut mantığı sağlamaz. Komutun mantığı, komutuyla ilişkilendirilir CommandBinding . Bazı denetimlerde bazı komutlar için yerleşik CommandBindings vardır. Bu mekanizma, gerçek uygulama değişirken bir komutun semantiğinin aynı kalmasını sağlar. TextBoxÖrneğin, Paste komutu görüntüleri destekleyecek şekilde tasarlanan bir denetimden farklı şekilde işler, ancak bir şeyi yapıştırmak için ne anlama geldiğini temel fikir aynı kalır. Komut mantığı komut tarafından sağlanamaz, ancak bunun yerine denetim veya uygulama tarafından sağlanması gerekir.
WPF 'deki pek çok denetim, komut kitaplığındaki bazı komutlar için yerleşik desteğe sahiptir. TextBoxÖrneğin,,,, ve gibi birçok uygulama düzenleme komutunu destekler PasteCopyCutRedoUndo . Uygulama geliştiricisinin bu denetimlerle çalışmak üzere bu komutları almak için özel bir şey yapması gerekmez. Komut yürütüldüğünde komut hedefi ise, TextBoxCommandBinding denetimde yerleşik olan öğesini kullanarak komutu işler.
Aşağıda komutun MenuItem hedefi olduğu, komutu için komut kaynağı olarak nasıl kullanılacağı gösterilmiştir PasteTextBox . Yapıştırmayı nasıl gerçekleştireceğini tanımlayan tüm mantık TextBox denetimin içinde yerleşiktir TextBox .
Bir MenuItem oluşturulur ve bu Command özelliği Paste komutuna ayarlanır. , CommandTarget Açıkça nesnesine ayarlı değildir TextBox . CommandTargetÖğesi ayarlanmamışsa, komutun hedefi, klavye odağına sahip olan öğedir. Klavye odağı olan öğe Paste komutu desteklemiyorsa veya şu anda Yapıştır komutunu yürütülemez (Pano boş olur), MenuItem gri olur.
<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