有没有办法通过代码中完成的键绑定将参数传递给命令?
我正在制作一个自定义控件,我需要添加一些默认的键绑定,微软已经完成了在文本框中复制和粘贴的操作。然而,其中一个键绑定需要将参数传递给它所绑定的命令。在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)
很糊涂小朋友2024-09-05 11:08:41
复制和粘贴命令由文本框处理,因此参数并未严格传递,但我知道您的意思。
我使用 hack - 和附加属性来执行此操作,就像
public class AttachableParameter : DependencyObject {
public static Object GetParameter(DependencyObject obj) {
return (Object)obj.GetValue(ParameterProperty);
}
public static void SetParameter(DependencyObject obj, Object value) {
obj.SetValue(ParameterProperty, value);
}
// Using a DependencyProperty as the backing store for Parameter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ParameterProperty =
DependencyProperty.RegisterAttached("Parameter", typeof(Object), typeof(AttachableParameter), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
}
在 xaml 中
<ListBox local:AttachableParameter.Parameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItems}" />
一样,它使参数成为选定的项目
,然后当命令在窗口上触发时,我使用它来查看命令参数是否存在(我从可以执行并执行)
private Object GetCommandParameter() {
Object parameter = null;
UIElement element = FocusManager.GetFocusedElement(this) as UIElement;
if (element != null) {
parameter = AttachableParameter.GetParameter(element as DependencyObject);
}
return parameter;
}
这是一个黑客,但我还没有找到另一种方法来获取从键绑定触发的绑定的命令参数。 (我很想知道更好的方法)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我找到了答案:
如果我的问题不清楚,我深表歉意。
I found the answer:
I apologize if my question was unclear.