2012-11-09 56 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); 
相关问题