WPF:如何使用命令和输入绑定 [英] WPF: How To Use Command & Input Bindings

查看:451
本文介绍了WPF:如何使用命令和输入绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令和在WPF中,输入绑定似乎非常复杂-将特定命令绑定到某些输入似乎并不总是有效。我应该如何去做?

Command & Input Bindings seem to be extremely complicated in WPF - binding specific commands to certain inputs don't always seem to work. How should I go about doing this?

-自我回答-

我已经用考虑我花了多长时间才能找到信息,理解并实际实施这些令人费解的事情,并根据我自己的发现做出个人回答。

I've updated with a personal answer with my own findings considering how long it's taken me to find information, understand, and actually implement these convoluted things.

WPF中的绑定似乎是一个不友好的概念,尤其是如果您没有经验的话。希望这会使人们的生活更轻松。

Bindings in WPF seem to be an unfriendly concept, especially if you have no experience. Hopefully this makes people's lives easier.

推荐答案

首先,在将使用绑定的xaml文件顶部添加应用程序名称空间:

First, add the application namespace at the top of xaml file that will use the binding: ex.

xmlns:main = clr-namespace:MyApplication

接下来,添加一个自定义的 static 类以包含命令,外部主窗口类:例如。

Next, add a custom static class to contain the commands, outside the main window class: ex.

public static class Command
{
    public static RoutedCommand GSToggleCmd = new RoutedCommand();
    public static RoutedCommand ScreenZoomCmd = new RoutedCommand();
}

我的主窗口类恰好是 MainWindow;我在其下面定义了Command类。

My main window class happened to be 'MainWindow'; I defined the Command class right below it.

最后,在xaml文件中添加命令绑定

Finally, add the command bindings in the xaml file

<Window.CommandBindings>
    <CommandBinding Command="main:Command.GSToggleCmd" Executed="GameStateToggleExecuted" />
    <CommandBinding Command="main:Command.ScreenZoomCmd" Executed="ApplyScreenFitCmd" />
</Window.CommandBindings>

Command = 是指<您将绑定到的code> RoutedCommand 。我已将名称空间引用命名为 main ,因此语法为 main:Command.nameofcommand

Command="" refers to the RoutedCommand that you'll be binding to. I've named my namespace reference main, hence the syntax main:Command.nameofcommand

Executed = 表示命令被触发时调用的函数。

Executed="" refers to the function called when the command is triggered.

执行函数的示例:

private void ApplyScreenFitCmd( object sender, ExecutedRoutedEventArgs args )
{
    string proportionStr = args.Parameter as string;
}

就是这样。在 WPF 中使用 CommandBindings 的一种简单,简单的方法。

And that's it. A minimalistic, simple approach to using CommandBindings in WPF.

要添加命令,只需在 Command <中添加新的静态 RoutedCommand / code>类,并将 CommandBinding 添加到 Window.CommandBindings

To add a command, just chuck in a new static RoutedCommand in the Command class and add the CommandBinding in the xaml file under Window.CommandBindings

注意:

Visual Studio的Xaml编辑器最初可能会抱怨找不到某些命令。

Visual Studio's Xaml editor may complain at first that some command cannot be found. Building the project will resolve the issue.

其他信息

您可以还可以通过 InputBindings 触发 CommandBindings 。 (关键触发器)

You can also trigger CommandBindings through InputBindings. (key triggers)

示例(放置在将使用它们的Xaml文件中):

Example (placed in the Xaml file that will use them):

<Window.InputBindings>
    <KeyBinding Key="F5" Command="main:Command.GSToggleCmd" />
    <KeyBinding Modifiers="Shift+Alt" Key="Q" Command="main:Command.ScreenZoomCmd" CommandParameter="1.0" />
    <KeyBinding Modifiers="Alt" Key="W" Command="main:Command.ScreenZoomCmd" CommandParameter="0.75" />
    <KeyBinding Modifiers="Alt" Key="E" Command="main:Command.ScreenZoomCmd" CommandParameter="0.5" />
</Window.InputBindings>

所以基本上是按下触发器 KeyBinding ,依次触发指定命令的 CommandBinding ,并触发相应的调用函数。

So it's basically the Key press triggers KeyBinding, which in turn triggers the CommandBinding of the command specified, triggering the corresponding call function.

与上面的示例一样,您还可以定义 CommandParameter 将参数发送到最终调用的函数。这样做的好处是,像上面一样,您可以重复使用相同的 CommandBinding ,只需插入不同的 CommandParameters

As with the above example, you can also define CommandParameter to send in a parameter to the function finally called. The nice thing about this is that, like the above, you can reuse the same CommandBinding by just chucking in different CommandParameters.

您还可以通过按钮,菜单项等来触发 CommandBindings

You can also trigger CommandBindings through Buttons, MenuItems, etc.

示例:

<MenuItem Header="100%" InputGestureText="Alt+Q" Command="main:Command.ScreenZoomCmd" CommandParameter="1.0"/>

这与 InputBindings 。

我花了一段时间才决定采用一种简约,统一的方式在WPF中使用绑定。我希望本文能防止此概念似乎容易引起的所有挣扎。

It took me a while before I settled down on a minimalistic, uniform way to use bindings in WPF. I hope this article prevents all the struggle that this concept seems to easily cause.

这篇关于WPF:如何使用命令和输入绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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