2009-06-15 34 views
7

我使用CommandManager.RegisterClassInputBinding將綁定添加到整個類型。現在我想刪除它。如何刪除通過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鍵 + ħ並註冊新的輸入爲Ctrl鍵 + 小號結合。如果我之前按Ctrl + H^按Ctrl +小號不工作,但是當我按下它之後。

我檢查sender.InputBindings和世界上只有一個結合(按Ctrl +小號),所以我的結論是,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); 
+0

您是否希望實施重點和絃像在Visual Studio如果是這樣,這?可能會有所幫助:http://kent-boogaart.com/blog/multikeygesture – CodeNaked 2016-02-26 02:39:09

回答

7

ApplicationCommands.NotACommand是專爲剛剛這一目的:

「T他的命令總是被忽略,並且不處理導致它的輸入事件。這提供了一種方法來關閉結合內置到現有的控制輸入「

要使用你的例子:

CommandManager.RegisterClassInputBinding(
    typeof(TextBox), 
    new InputBinding(
     ApplicationCommands.NotACommand, 
     new KeyGesture(Key.S, ModifierKeys.Control))); 
+0

看起來很有缺陷,我正在寫快捷方式管理器,它允許用戶添加/刪除輸入綁定。據我所知,如果用戶刪除綁定​​爲你建議綁定將仍然存在,並且每次用戶都會累積,這是否正確? – 2009-06-16 09:16:26