中继/代表命令和输入手势

发布于 2024-09-15 11:30:25 字数 916 浏览 4 评论 0原文

我想从我的视图模型中获得与 F8 关联的命令,并且不太了解输入手势。我必须将其包装在路由命令中还是有其他推荐的方法来执行此操作?

干杯,
Berryl

更新

我原来的帖子确实有两个痛点。第一个是在 WPF 4 之前,您无法将命令直接绑定到输入手势或依赖命令的 DataContext 的视觉继承,并且必须通过 CommandManager 层。 问题已得到修复,因为 Command 是 InputBinding 上的附加 DP,因此您需要做的就是:

<DataGrid.InputBindings>
    <KeyBinding Command="{Binding MyViewModel.MyCommand}" Key="F8" />
</DataGrid.InputBindings>

这似乎是 NVM 指出的 MVVM Toolkit 的 CommandReference 有用的原因,但现在在 WPF 4 中,这个 认识到将输入手势绑定到命令与将该手势的文本表示关联到菜单项不同。所以我需要做这样的事情:

<MenuItem Header="{x:Static s:Strings.MyHeaderString}" Command="{Binding MyViewModel.MyCommand}" 
    InputGestureText="F8"/>

我把这个拼写出来,希望有人会告诉我我做错了(或对了)。假设这是您能做的最好的事情,那么这取决于您可以获得多少 DRYness 以及如何做到这一点,同时将严格的视觉元素保留在 ViewModel 之外。您可以通过在命令上添加一些属性(即 KeyGesture、KeyModifier)或在资源文件中添加“F8”(我想)来实现此目的,具体取决于您需要重用某些内容的程度和您的观点。还有关于该命令有多少个输入手势!

I want to have a command from my view model associated with F8, and don't know a great deal about input gestures. Must I wrap it in a routed command or is there another recommended way to do this?

Cheers,
Berryl

UPDATE

My original posting really had two pain points in them. The first was that prior to WPF 4 you couldn't bind a command directly to an input gesture or count on visual inheritance for the command's DataContext, and had to go through a CommandManager layer. That seems like the reason the MVVM Toolkit's CommandReference that NVM pointed out was useful, but now with WPF 4 this is fixed as the Command is an attached DP on the InputBinding, so all you need to do is something like:

<DataGrid.InputBindings>
    <KeyBinding Command="{Binding MyViewModel.MyCommand}" Key="F8" />
</DataGrid.InputBindings>

The second pain point to realize is that binding an input gesture to a command is not the same as associating the textual representation of that gesture to a menu item. So I need to do something like:

<MenuItem Header="{x:Static s:Strings.MyHeaderString}" Command="{Binding MyViewModel.MyCommand}" 
    InputGestureText="F8"/>

I spelled this out in the hopes that someone will either tell me I've got something wrong (or right). Assuming this is the best you can do then it's down to how much DRYness you can get and how to do that while keeping strictly visual elements out of your ViewModels. You could do this either by having some properties on your Command (ie, KeyGesture, KeyModifier) or having the "F8" in a resource file I suppose, depending on how much you need to reuse something and your point of view. Also on how many input gestures you have for the command!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心凉怎暖 2024-09-22 11:30:25

如何将按键与复合 WPF 中的 DelegateCommand?

这就是您的答案。您应该使用 Gesture="F8" 而不是 Key="X"

How do I associate a keypress with a DelegateCommand in Composite WPF?

This is your answer. Instead of Key="X" you should use Gesture="F8"

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