CommandManager.RegisterClassInputBinding を使用して、型全体にバインドを追加しました。今、私はそれを取り除きたいです。
これは私がテストしたものです。
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
{
CommandManager.RegisterClassInputBinding(
typeof(TextBox),
new InputBinding(TestCommand, new KeyGesture(Key.S, ModifierKeys.Control)));
MessageBox.Show("CommandBinding_Executed_1");
}
このメソッドは + で呼び出され、+CtrlのH新しい入力バインディングを登録します。+の前に+を押すと機能しませんが、後に押すと機能します。CtrlSCtrlSCtrlH
チェックsender.InputBindingsしたところ、バインディング ( Ctrl+ ) が 1 つしかなかったので、既存のすべてのインスタンスにバインディングを追加するのではなく、クラスに関連付けられたバインディングを保存し、それらを処理されたジェスチャと比較するとS結論付けました。RegisterClassInputBinding()
しかし、なぜRemoveClassInputBinding()方法がないのですか?:(
編集
リフレクションを通じて意図したことを行うことさえできましたが、実装するのは簡単ですが、そのためのネイティブメソッドを見つけることができません。
var fieldInfo = typeof(CommandManager).GetField(
"_classInputBindings", BindingFlags.Static | BindingFlags.NonPublic);
var fieldData = (HybridDictionary)fieldInfo.GetValue(null);
var inputBindingCollection = (InputBindingCollection)fieldData[typeof(TextBox)];
foreach (var o in inputBindingCollection)
{
if (o == inputBinding)
{
MessageBox.Show("half way there");
}
}
inputBindingCollection.Remove(inputBinding);