【问题标题】:Detect which Control is Focused?检测哪个控件被聚焦?
【发布时间】:2011-11-28 10:49:32
【问题描述】:

我想用一个快捷键名称为两个按钮设置两个快捷键。
如何检测哪个控件集中在 MVVM 中?

【问题讨论】:

  • 为什么需要检测集中控制?热键绑定到命令,而不是控制。
  • ViewModel 不应该知道或关心谁有焦点。

标签: c# wpf xaml mvvm keyboard-shortcuts


【解决方案1】:

您可以简单地将热键添加到任何合乎逻辑的范围以使其存在。有很多方法可以处理 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>

如果UserControlA 有键盘焦点,这将运行SaveACommand,或者如果UserControlB 有键盘焦点,则运行SaveBCommand

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2010-12-29
    • 2019-05-28
    相关资源
    最近更新 更多