方法 : コマンドをサポートするコントロールにコマンドをフックする

コマンドのサポートが組み込まれた ControlRoutedCommand をフックする方法を次の例に示します。 コマンドを複数のソースにフックするサンプル全体については、カスタム RoutedCommand の作成のサンプルを参照してください。

使用例

Windows Presentation Foundation (WPF) には、アプリケーション プログラマがよく使用する一般的なコマンドのライブラリが用意されています。 このコマンド ライブラリには、ApplicationCommandsComponentCommandsNavigationCommandsMediaCommands、および EditingCommands クラスが含まれています。

これらのクラスを構成する静的な RoutedCommand オブジェクトには、コマンド ロジックが用意されていません。 コマンドのロジックは、CommandBinding でコマンドに関連付けられます。 一部のコントロールには、コマンドの CommandBindings が組み込まれています。 これにより、コマンドの意味は変わりませんが、実際の実装は変わる場合があります。 たとえば、TextBox は、Paste コマンドの処理方法は、イメージをサポートするよう設計されたコントロールとは異なります。ただし、何かを貼り付けるという基本的な概念は変わりません。 コマンド ロジックはコマンドでは提供できませんが、コントロールまたはアプリケーションで提供する必要があります。

WPF の多くのコントロールには、コマンド ライブラリにある一部のコマンドのサポートが組み込まれています。 たとえば、TextBox は、PasteCopyCutRedoUndo などの多くのアプリケーション編集コマンドをサポートしています。 アプリケーション開発者は、コントロールで使用するこれらのコマンドを取得するのに特別な作業を行う必要はありません。 TextBox がコマンド ターゲットである場合は、コマンドを実行すると、コントロールに組み込まれている CommandBinding を使用してコマンドを処理します。

MenuItemPaste コマンドのコマンド ソースとして (コマンドの対象は TextBox) 使用する方法を次の例に示します。 TextBox が貼り付けを実行する方法を定義するロジックは、すべて TextBox コントロールに組み込まれています。

MenuItem が作成され、その Command プロパティが Paste コマンドに設定されます。 CommandTarget が明示的に TextBox オブジェクトに設定されることはありません。 CommandTarget が設定されていない場合、コマンドの対象はキーボード フォーカスを持つ要素になります。 キーボード フォーカスを持つ要素が Paste コマンドをサポートしていなか、または貼り付けコマンドを現在実行できない場合 (クリップボードが空の場合など)、MenuItem は灰色表示されます。

<Window x:Class="SDKSamples.Window1"
    xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="https://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 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
// 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);
}

参照

処理手順

方法 : コマンドをサポートしないコントロールにコマンドをフックする

概念

コマンド実行の概要