在搜索此网站时,我发现了这篇文章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);
这里唯一缺少的部分是如何将键绑定命令的参数绑定到我的文本框的文本,我似乎无法在任何地方找到答案。帮助将不胜感激。
答案 0 :(得分: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);