2011-11-28 40 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