我想为两个具有一个快捷方式名称的按钮设置两个快捷方式。
我如何检测哪个控件集中在 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/