检测哪个控件是重点?

发布于 2024-12-18 17:12:33 字数 59 浏览 2 评论 0原文

我想为具有一个快捷键名称的两个按钮设置两个快捷键。
我如何检测 MVVM 中关注的是哪个控件?

I want to set two shortkeys for two buttons with one shortkey name.
How can i detect which control is focused in MVVM ?

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

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

发布评论

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

评论(1

狼亦尘 2024-12-25 17:12:34

您只需将热键添加到其存在的任何逻辑范围即可。处理 WPF 热键的方法有很多,但是您的基本标记应如下所示:

<Window>
    <StackPanel>
        <local:MyUserControlA>
            <local:MyUserControlA.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding SaveACommand}" />
            </local:MyUserControlA.InputBindings>
        </local:MyUserControlA>

        <local:MyUserControlB>
            <local:MyUserControlB.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding SaveBCommand}" />
            </local:MyUserControlB.InputBindings>
        </local:MyUserControlB>

    </StackPanel>
</Window>

这将运行 SaveACommand if < code>UserControlA 具有键盘焦点,或者 SaveBCommand(如果 UserControlB 具有键盘焦点)。

You can simply add the hotkey to whatever scope is logical for it to exist in. There are many ways to handle hotkeys to WPF, however your basic markup should look something like this:

<Window>
    <StackPanel>
        <local:MyUserControlA>
            <local:MyUserControlA.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding SaveACommand}" />
            </local:MyUserControlA.InputBindings>
        </local:MyUserControlA>

        <local:MyUserControlB>
            <local:MyUserControlB.InputBindings>
                <KeyBinding Key="Enter" Command="{Binding SaveBCommand}" />
            </local:MyUserControlB.InputBindings>
        </local:MyUserControlB>

    </StackPanel>
</Window>

This will run SaveACommand if UserControlA has keyboard focus, or SaveBCommand if UserControlB has keyboard focus.

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