以编程方式为文本框输入创建按键绑定 [英] Programmatically create a key binding for textbox input

查看:64
本文介绍了以编程方式为文本框输入创建按键绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索该网站时,我发现了该帖子创建一个WPF中简单,未经修改的键绑定,其中显示了如何将简单命令绑定到a键.但是,我需要的还不止于此.我还想为此命令设置一个参数.我将使用绑定到文本框的参数作为输入. 这是我的代码:

While searching this site, I've found this post Create a simple, unmodified key binding in WPF which shows how to bind a simple command to the a key. However I require a bit more than this. I'd like to also set a parameter to this command. A parameter which is will be bound to a text box, I use for input. Here's the code I have:

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);

这里唯一缺少的部分是如何将键绑定命令的参数绑定到我的文本框的文本,我似乎无法在任何地方找到答案.帮助将不胜感激.

The only missing piece here is how to bind the parameter of the key binding's command to the text of my textbox, and I cant seem to find the answer anywhere. Help would be appreciated.

推荐答案

        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);

这篇关于以编程方式为文本框输入创建按键绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆