2015-01-01 50 views
0

我在ContentControl內有UserControl。當我按下Escape鍵時,我想讓用戶控制關閉。所以我用一個KeyBinding這樣的:KeyBindings WPF未在負載上開火

<UserControl.CommandBindings> 
    <CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" /> 
</UserControl.CommandBindings> 

<UserControl.InputBindings> 
    <KeyBinding Key="Escape" Command="{StaticResource CloseCommand}" /> 
</UserControl.InputBindings> 

當按下鍵Escape,這個代碼應該火CloseCommand。但是,第一次加載UserControl時,它不會觸發。如果我使用另一個UserControl導航並更改ContentControl內容,那麼它可以工作。

有什麼想法是怎麼回事?

回答

2

當您按下退出鍵時,您的控制功能可能並不重要。 您應該將UserControlFocusable屬性設置爲True,然後在UserControl的加載事件中調用Focus()方法。

+0

真的!非常感謝! – Sonhja

+0

不客氣! –