c# - 检测哪个控件被聚焦?

标签 c# wpf xaml mvvm keyboard-shortcuts

我想为两个具有一个快捷方式名称的按钮设置两个快捷方式。
我如何检测哪个控件集中在 MVVM 中?

最佳答案

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

关于c# - 检测哪个控件被聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8294702/

相关文章:

c# - 焦点抽象

c# - WPF 多个 ID 与 Datagrid 复选框

c# - ProgressRing IsActive 属性未更新

c# - 如何在 MVVM 中的不同 View 之间同步(模型)数据

c# - 在 Dynamics crm 中获取用户的上下文

C# AWS S3 上传损坏的文件

c# - 如何转换 CreateInstance 的返回值

c# - WPF 中的 ValidationRule 与行为

WPF-如何在 <Image> 控件中显示 ImageSource 的裁剪区域?

c# - 如何从打印机变量中获取纸张大小?