有没有办法通过代码中完成的键绑定将参数传递给命令?
我正在制作一个自定义控件,我需要添加一些默认的键绑定,微软已经完成了在文本框中复制和粘贴的操作。然而,其中一个键绑定需要将参数传递给它所绑定的命令。在xaml中执行此操作很简单,有什么方法可以在代码中执行此操作吗?
this.InputBindings.Add(new KeyBinding(ChangeToRepositoryCommand, new KeyGesture(Key.F1)));
I am making a custom control I need to add some default keybindings, microsoft has already done with copy and paste in a textbox. However one of the keybindings needs to pass a parameter to the command which it is bound to. It is simple to do this in xaml, is there any way to do this in code?
this.InputBindings.Add(new KeyBinding(ChangeToRepositoryCommand, new KeyGesture(Key.F1)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案:
如果我的问题不清楚,我深表歉意。
I found the answer:
I apologize if my question was unclear.
复制和粘贴命令由文本框处理,因此参数并未严格传递,但我知道您的意思。
我使用 hack - 和附加属性来执行此操作,就像
在 xaml 中
一样,它使参数成为选定的项目
,然后当命令在窗口上触发时,我使用它来查看命令参数是否存在(我从可以执行并执行)
这是一个黑客,但我还没有找到另一种方法来获取从键绑定触发的绑定的命令参数。 (我很想知道更好的方法)
The copy and paste commands are handled by the text box so parameters are not strictly passed, but i know what you are getting at.
I do this using a hack - and an attached property, like so
then in the xaml
which makes the parameter the selected items
then when the command fires on the window i use this to see if the command parameter is there (i call this from the can execute and the Executed)
It is a hack, but i have not found another way to get the command parameter for a binding that is fired from a key binding. (I would love to know a better way)