2011-11-28 41 views
3

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

+4

为什么你需要检测集中控制?热键必然要命令,而不是控制。 – icebat

+0

ViewModel不应该知道或关心谁有重点。 – Will

回答

0

您可以简单地添加了热键,无论范围是合乎逻辑的它在存在有许多方法来处理热键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