2012-11-09 75 views
0

在搜索此站點時,我發現此帖爲Create a simple, unmodified key binding in WPF,它顯示瞭如何將簡單命令綁定到某個鍵。不過,我需要的不僅僅是這一點。我想也爲這個命令設置一個參數。一個參數將被綁定到一個文本框,我用於輸入。 這裏是我的代碼:以編程方式爲文本框輸入創建鍵綁定

var textBinding = new Binding("Text") { Source = textBoxInput }; 
      buttonConfirmAddKitType.SetBinding(ButtonBase.CommandParameterProperty, textBinding); 
      var keybinding = new KeyBinding 
             { 
              Key = Key.Enter, 
              Command = command, 
             }; 
      //Here I need a way to set the source of the command to the text of the input textbox, as done above 
      textBoxInput.InputBindings.Add(keybinding); 

這裏唯一缺少的是如何把關鍵命令結合對我的文本框的文本的綁定參數,而我似乎無法在任何地方找到答案。幫助將不勝感激。

回答

1
 var textBinding = new Binding("Text") { Source = textBoxInput }; 
     buttonConfirmAddKitType.SetBinding(ButtonBase.CommandParameterProperty, textBinding); 
     var keybinding = new KeyBinding 
     { 
      Key = Key.Enter, 
      Command = command, 
     }; 
     keybinding.CommandParameter = textBoxInput.Text; 
     textBoxInput.InputBindings.Add(keybinding); 
相關問題