InputBinding クラス

定義

InputGesture とコマンドの間のバインディングを表します。 コマンドは、RoutedCommand の可能性があります。

public ref class InputBinding : System::Windows::DependencyObject, System::Windows::Input::ICommandSource
public ref class InputBinding : System::Windows::Freezable, System::Windows::Input::ICommandSource
public class InputBinding : System.Windows.DependencyObject, System.Windows.Input.ICommandSource
public class InputBinding : System.Windows.Freezable, System.Windows.Input.ICommandSource
type InputBinding = class
    inherit DependencyObject
    interface ICommandSource
type InputBinding = class
    inherit Freezable
    interface ICommandSource
Public Class InputBinding
Inherits DependencyObject
Implements ICommandSource
Public Class InputBinding
Inherits Freezable
Implements ICommandSource
継承
継承
派生
実装

次の例では、 を使用 KeyBinding して をコマンドにバインド KeyGesture する方法を Open 示します。 キー ジェスチャが実行されると、Open コマンドが呼び出されます。

<Window.InputBindings>
  <KeyBinding Key="B"
              Modifiers="Control" 
              Command="ApplicationCommands.Open" />
</Window.InputBindings>

次の例では、カスタム コマンドを オブジェクトにバインドする方法を InputBinding 示します。 次の例では、次のいずれかの操作を実行して、ユーザーが背景色を変更できるようにするアプリケーションを作成します。

  • ボタンをクリックしています。

  • Ctrl + C キーを押します。

  • を右クリックします StackPanel (の ListBox外側)。

最初の例では、 という名前 SimpleDelegateCommandのクラスを作成します。 このクラスは、コマンドを作成するオブジェクトがコマンドの実行時に発生するアクションを定義できるように、デリゲートを受け入れます。 SimpleDelegateCommand また、コマンドを呼び出すキーとマウス入力を指定するプロパティも定義します。 GestureKeyキーボード入力MouseGestureを指定しますGestureModifier。マウス入力を指定します。

 // Create a class that implements ICommand and accepts a delegate.
public class SimpleDelegateCommand : ICommand
{
    // Specify the keys and mouse actions that invoke the command. 
    public Key GestureKey { get; set; }
    public ModifierKeys GestureModifier { get; set; }
    public MouseAction MouseGesture { get; set; }

    Action<object> _executeDelegate;

    public SimpleDelegateCommand(Action<object> executeDelegate)
    {
        _executeDelegate = executeDelegate;
    }

    public void Execute(object parameter)
    {
        _executeDelegate(parameter);
    }

    public bool CanExecute(object parameter) { return true; }
    public event EventHandler CanExecuteChanged;
}
' Create a class that implements ICommand and accepts a delegate. 
Public Class SimpleDelegateCommand
    Implements ICommand

    ' Specify the keys and mouse actions that invoke the command. 
    Private _GestureKey As Key
    Private _GestureModifier As ModifierKeys
    Private _MouseGesture As MouseAction

    Public Property GestureKey() As Key
        Get
            Return _GestureKey
        End Get
        Set(ByVal value As Key)
            _GestureKey = value
        End Set
    End Property

    Public Property GestureModifier() As ModifierKeys
        Get
            Return _GestureModifier
        End Get
        Set(ByVal value As ModifierKeys)
            _GestureModifier = value
        End Set
    End Property

    Public Property MouseGesture() As MouseAction
        Get
            Return _MouseGesture
        End Get
        Set(ByVal value As MouseAction)
            _MouseGesture = value
        End Set
    End Property

    Private _executeDelegate As Action(Of Object)

    Public Sub New(ByVal executeDelegate As Action(Of Object))
        _executeDelegate = executeDelegate
    End Sub

    Public Sub Execute(ByVal parameter As Object) _
        Implements ICommand.Execute

        _executeDelegate(parameter)
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean _
        Implements ICommand.CanExecute

        Return True
    End Function

    Public Event CanExecuteChanged As EventHandler _
        Implements ICommand.CanExecuteChanged
End Class

次の例では、 である をColorChangeCommandSimpleDelegateCommand作成して初期化します。 この例では、コマンドが呼び出されたときに実行されるメソッドも定義し、および プロパティをGestureKeyGestureModifierMouseGesture設定します。 アプリケーションは、 の InitializeCommand コンストラクターなど、プログラムの開始時に メソッドを Window呼び出します。

public SimpleDelegateCommand ChangeColorCommand
{
    get { return changeColorCommand; }
}

private SimpleDelegateCommand changeColorCommand;

private void InitializeCommand()
{
    originalColor = this.Background;

    changeColorCommand = new SimpleDelegateCommand(x => this.ChangeColor(x));

    DataContext = this;
    changeColorCommand.GestureKey = Key.C;
    changeColorCommand.GestureModifier = ModifierKeys.Control;
    ChangeColorCommand.MouseGesture = MouseAction.RightClick;
}

private Brush originalColor, alternateColor;

// Switch the Background color between
// the original and selected color.
private void ChangeColor(object colorString)
{
    if (colorString == null)
    {
        return;
    }

    Color newColor = 
        (Color)ColorConverter.ConvertFromString((String)colorString);
    
    alternateColor = new SolidColorBrush(newColor);

    if (this.Background == originalColor)
    {
        this.Background = alternateColor;
    }
    else
    {
        this.Background = originalColor;
    }
}
Public ReadOnly Property ChangeColorCommand() As SimpleDelegateCommand
    Get
        Return _changeColorCommand
    End Get
End Property

Private _changeColorCommand As SimpleDelegateCommand
Private originalColor As Brush, alternateColor As Brush

Private Sub InitializeCommand()
    originalColor = Me.Background

    _changeColorCommand = New SimpleDelegateCommand(Function(x) Me.ChangeColor(x))

    DataContext = Me
    _changeColorCommand.GestureKey = Key.C
    _changeColorCommand.GestureModifier = ModifierKeys.Control
    _changeColorCommand.MouseGesture = MouseAction.RightClick
End Sub

' Switch the Background color between 
' the original and selected color. 
Private Function ChangeColor(ByVal colorString As Object) As Integer

    If colorString Is Nothing Then
        Return 0
    End If

    Dim newColor As Color = DirectCast(ColorConverter.ConvertFromString(DirectCast(colorString, [String])), Color)

    alternateColor = New SolidColorBrush(newColor)

    If Brush.Equals(Me.Background, originalColor) Then
        Me.Background = alternateColor
    Else
        Me.Background = originalColor
    End If

    Return 0
End Function

最後に、次の例では、ユーザー インターフェイスを作成します。 この例では、 KeyBinding と をMouseBinding含む に StackPanel と をButtonListBox追加します。 ユーザーが で ListBox項目を選択すると、背景の色を選択した色に変更できます。 いずれの場合も、 CommandParameter プロパティは 内の ListBox選択した項目にバインドされ、 Command プロパティは に ColorChangeCommandバインドされます。 、KeyBinding.KeyKeyBinding.Modifiers、および MouseBinding.MouseAction プロパティは、 クラスの対応するプロパティにSimpleDelegateCommandバインドされます。

<StackPanel Background="Transparent">
  <StackPanel.InputBindings>
    
    <KeyBinding Command="{Binding ChangeColorCommand}"
                CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}"
                Key="{Binding ChangeColorCommand.GestureKey}"
                Modifiers="{Binding ChangeColorCommand.GestureModifier}"/>

    <MouseBinding Command="{Binding ChangeColorCommand}"
                  CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}"
                  MouseAction="{Binding ChangeColorCommand.MouseGesture}"/>
  
  </StackPanel.InputBindings>
  
  <Button Content="Change Color" 
          Command="{Binding ChangeColorCommand}" 
          CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}">
  </Button>

  <ListBox Name="colorPicker"
           Background="Transparent"
           xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String>Red</sys:String>
    <sys:String>Green</sys:String>
    <sys:String>Blue</sys:String>
    <sys:String>Yellow</sys:String>
    <sys:String>Orange</sys:String>
    <sys:String>Purple</sys:String>
  </ListBox>
</StackPanel>

注釈

を作成 InputBindingすることで、ユーザー入力がコマンドを呼び出すように指定できます。 ユーザーが指定した入力を実行すると、 ICommand プロパティに Command 設定されている が実行されます。

オブジェクトInputBindingで定義されているコマンドを呼び出すように指定するには、 プロパティCommandParameter、、および CommandTarget プロパティにバインドをCommand作成します。 これにより、カスタム コマンドを定義し、ユーザー入力に関連付けることができます。 詳細については、「例」セクションの 2 番目の例を参照してください。

InputBinding 、 を に登録 RegisterClassInputBinding することで、特定のオブジェクトまたはクラス レベルで CommandManager定義できます。

クラス自体は InputBinding 、パブリックパラメーターなしのコンストラクターを公開しないため、XAML の使用をサポートしていません (パラメーターなしのコンストラクターがありますが、保護されています)。 ただし、派生クラスはパブリック コンストラクターを公開できるため、XAML を使用して継承される InputBinding 派生クラスのプロパティを設定できます。 XAML でインスタンス化でき、XAML でプロパティを設定できる 2 つの既存InputBindingの派生クラスは と MouseBindingですKeyBinding。 XAML で設定され、値が プロパティとして 1 つ以上 InputBinding のオブジェクトを受け取る WPF プログラミングの UIElement.InputBindings 一般的なプロパティ。

XAML オブジェクト要素の使用方法

<inputBindingDerivedClass.../>

XAML 値

inputBindingDerivedClass
MouseBindingなどのKeyBindingオブジェクト要素構文InputBindingをサポートする の派生クラス。 「解説」を参照してください。

コンストラクター

InputBinding()

InputBinding から派生したクラスの基本の初期化を行います。

InputBinding(ICommand, InputGesture)

コマンドおよび入力ジェスチャを指定して、InputBinding クラスの新しいインスタンスを初期化します。

フィールド

CommandParameterProperty

CommandParameter 依存関係プロパティを識別します。

CommandProperty

Command 依存関係プロパティを識別します。

CommandTargetProperty

CommandTarget 依存関係プロパティを識別します。

プロパティ

CanFreeze

オブジェクトを変更不可能にできるかどうかを示す値を取得します。

(継承元 Freezable)
Command

この入力バインドに関連付けられる ICommand を取得または設定します。

CommandParameter

特定のコマンドのコマンド固有のデータを取得または設定します。

CommandTarget

コマンドの対象要素を取得または設定します。

DependencyObjectType

このインスタンスの DependencyObjectType CLR 型をラップする を取得します。

(継承元 DependencyObject)
Dispatcher

この Dispatcher が関連付けられている DispatcherObject を取得します。

(継承元 DispatcherObject)
Gesture

この入力バインドに関連付けられる InputGesture を取得または設定します。

IsFrozen

オブジェクトが変更可能かどうかを示す値を取得します。

(継承元 Freezable)
IsSealed

このインスタンスが現在シールされている (読み取り専用である) かどうかを示す値を取得します。

(継承元 DependencyObject)

メソッド

CheckAccess()

呼び出し元のスレッドがこの DispatcherObject にアクセスできるかどうかを確認します。

(継承元 DispatcherObject)
ClearValue(DependencyProperty)

プロパティのローカル値をクリアします。 クリアするプロパティは DependencyProperty 識別子で指定されます。

(継承元 DependencyObject)
ClearValue(DependencyPropertyKey)

読み取り専用プロパティのローカル値を消去します。 消去するプロパティは、DependencyPropertyKey で指定します。

(継承元 DependencyObject)
Clone()

Freezable の変更可能な複製を作成し、オブジェクトの値の詳細コピーを作成します。 このメソッドは、オブジェクトの依存関係プロパティをコピーするときに式をコピーしますが (コピーされた式は解決されなくなる場合があります)、アニメーションやその現在の値はコピーしません。

(継承元 Freezable)
CloneCore(Freezable)

指定されたオブジェクトのプロパティの基本 (アニメーション化されていない) 値をコピーします。

CloneCurrentValue()

Freezable の現在の値を使用して、変更可能な複製 (詳細コピー) を作成します。

(継承元 Freezable)
CloneCurrentValueCore(Freezable)

指定されたオブジェクトのプロパティの現在の値をコピーします。

CoerceValue(DependencyProperty)

指定した依存関係プロパティの値を強制します。 これは、呼び出し元の DependencyObject の依存関係プロパティのプロパティ メタデータで指定されている CoerceValueCallback 関数を呼び出すことによって実現されます。

(継承元 DependencyObject)
CreateInstance()

Freezable クラスの新しいインスタンスを初期化します。

(継承元 Freezable)
CreateInstanceCore()

InputBinding のインスタンスを作成します。

Equals(Object)

指定した DependencyObject が現在の DependencyObject と等しいかどうかを判断します。

(継承元 DependencyObject)
Freeze()

現在のオブジェクトを変更不可能にし、その IsFrozen プロパティを true に設定します。

(継承元 Freezable)
FreezeCore(Boolean)

Freezable オブジェクトを変更不可能な状態にするか、変更不可能な状態にできるかどうかをテストします。

(継承元 Freezable)
GetAsFrozen()

基本プロパティ値 (アニメーション化されていない値) を使用して、Freezable の 固定されたコピーを作成します。 コピーが固定されているため、参照によって任意の固定されたサブオブジェクトがコピーされます。

(継承元 Freezable)
GetAsFrozenCore(Freezable)

基本プロパティ値 (アニメーション化されていない値) を使用して、このインスタンスを、指定した Freezable の固定された複製にします。

GetCurrentValueAsFrozen()

現在のプロパティ値を使用して、Freezable の固定されたコピーを作成します。 コピーが固定されているため、参照によって任意の固定されたサブオブジェクトがコピーされます。

(継承元 Freezable)
GetCurrentValueAsFrozenCore(Freezable)

現在のインスタンスを、指定した Freezable の固定された複製にします。 オブジェクトに、アニメーション化された依存関係プロパティが存在する場合、現在アニメーション化されている値がコピーされます。

GetHashCode()

この DependencyObject のハッシュ コードを取得します。

(継承元 DependencyObject)
GetLocalValueEnumerator()

どの依存関係プロパティがこの DependencyObject 上にローカルに設定された値を持つかを確認するための、専用の列挙子を作成します。

(継承元 DependencyObject)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
GetValue(DependencyProperty)

DependencyObject のこのインスタンスにある依存関係プロパティの現在の有効値を返します。

(継承元 DependencyObject)
InvalidateProperty(DependencyProperty)

指定した依存関係プロパティの有効値を再評価します。

(継承元 DependencyObject)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnChanged()

現在の Freezable オブジェクトの変更時に呼び出されます。

(継承元 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject)

設定されたばかりの DependencyObjectType データ メンバーに対して、適切なコンテキスト ポインターが確立されていることを確認します。

(継承元 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

このメンバーは、Windows Presentation Foundation (WPF) インフラストラクチャをサポートしており、コードから直接使用することを意図したものではありません。

(継承元 Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

この DependencyObject の依存関係プロパティの有効値が更新された場合に必ず呼び出されます。 変更された特定の依存関係プロパティは、イベント データで報告されます。

(継承元 DependencyObject)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

OnPropertyChanged(DependencyPropertyChangedEventArgs)DependencyObject 実装をオーバーライドして、さらに型 Freezable の変化する依存関係プロパティへの応答として任意の Changed ハンドラーも呼び出します。

(継承元 Freezable)
ReadLocalValue(DependencyProperty)

ローカルの依存関係プロパティの値を返します (存在する場合)。

(継承元 DependencyObject)
ReadPreamble()

Freezable が有効なスレッドからアクセスされていることを確認します。 Freezable の継承側は、依存関係プロパティでないデータ メンバーを読み取る任意の API の開始時に、このメソッドを呼び出す必要があります。

(継承元 Freezable)
SetCurrentValue(DependencyProperty, Object)

依存関係プロパティ値のソースを変更せずにその値を設定します。

(継承元 DependencyObject)
SetValue(DependencyProperty, Object)

依存関係プロパティ識別子を指定して、該当する依存関係プロパティのローカル値を設定します。

(継承元 DependencyObject)
SetValue(DependencyPropertyKey, Object)

依存関係プロパティの DependencyPropertyKey 識別子で指定した読み取り専用の依存関係プロパティのローカル値を設定します。

(継承元 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

シリアル化プロセスが、指定された依存関係プロパティの値をシリアル化する必要があるかどうかを示す値を返します。

(継承元 DependencyObject)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
VerifyAccess()

呼び出し元のスレッドがこの DispatcherObject にアクセスできるように強制します。

(継承元 DispatcherObject)
WritePostscript()

FreezableChanged イベントを発生させ、その OnChanged() メソッドを呼び出します。 Freezable から派生するクラスは、依存関係プロパティとして格納されていないクラス メンバーを変更するすべての API の終了時に、このメソッドを呼び出す必要があります。

(継承元 Freezable)
WritePreamble()

Freezable が固定されておらず、有効なスレッド コンテキストからアクセスされていることを確認します。 Freezable の継承側は、依存関係プロパティでないデータ メンバーに書き込む任意の API の開始時に、このメソッドを呼び出す必要があります。

(継承元 Freezable)

イベント

Changed

Freezable、またはこれに含まれているオブジェクトが変更されると発生します。

(継承元 Freezable)

適用対象

こちらもご覧ください