有没有办法通过代码中完成的键绑定将参数传递给命令?

发布于 2024-08-29 11:08:41 字数 231 浏览 3 评论 0原文

我正在制作一个自定义控件,我需要添加一些默认的键绑定,微软已经完成了在文本框中复制和粘贴的操作。然而,其中一个键绑定需要将参数传递给它所绑定的命令。在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2024-09-05 11:08:41

我找到了答案:

InputBindings.Add(new KeyBinding(ChangeToRepositoryCommand, new KeyGesture(Key.F1)) { CommandParameter = 0 });

如果我的问题不清楚,我深表歉意。

I found the answer:

InputBindings.Add(new KeyBinding(ChangeToRepositoryCommand, new KeyGesture(Key.F1)) { CommandParameter = 0 });

I apologize if my question was unclear.

很糊涂小朋友 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;
  }

这是一个黑客,但我还没有找到另一种方法来获取从键绑定触发的绑定的命令参数。 (我很想知道更好的方法)

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

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

then in the xaml

<ListBox local:AttachableParameter.Parameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItems}" />

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)

  private Object GetCommandParameter() {
     Object parameter = null;
     UIElement element = FocusManager.GetFocusedElement(this) as UIElement;
     if (element != null) {
        parameter = AttachableParameter.GetParameter(element as DependencyObject);
     }
     return parameter;
  }

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文