Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
Aşağıdaki örnek, yerleşik komut desteği olan bir RoutedCommand'e bir Control'ın nasıl bağlanacağını göstermektedir. Birden çok kaynağa komutları bağlayan eksiksiz bir örnek için Özel Yönlendirilmiş Komut Örneği Oluşturma örneğine bakın.
Ö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 nesneleri komut mantığı sağlamaz. Komutun mantığı, bir CommandBindingile komutla ilişkilendirilmiştir. 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. Örneğin TextBox, Paste komutunu 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. örneğin TextBox, Paste, Copy, Cut, Redove Undogibi uygulama 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. komut yürütülürken TextBox komut hedefiyse, denetimin içinde yerleşik olarak bulunan CommandBinding kullanarak komutu işler.
Aşağıda, MenuItem komutu için komut kaynağı olarak bir Paste nasıl kullanılacağı gösterilmektedir ve burada bir TextBox komutun hedefidir. TextBox yapıştırma işlemini nasıl gerçekleştirdiğini tanımlayan tüm mantık, TextBox denetiminde yerleşik olarak bulunur.
bir MenuItem oluşturulur ve Command özelliği Paste komutuna ayarlanır. CommandTarget açıkça TextBox nesnesine ayarlanmadı. CommandTarget ayarlanmadığında, komutun hedefi klavye odağı olan öğedir. Klavye odağı olan öğe Paste komutunu desteklemiyorsa 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 bakınız
.NET Desktop feedback