在wpf中弹出键绑定

时间:2014-09-30 10:43:42

标签: c# .net wpf

是否可以绑定在wpf弹出窗口中按下的键?

我这样尝试,但似乎没有效果:

<Popup.InputBindings>
   <KeyBinding Key="Escape" Command="Close" />
   <KeyBinding Key="Enter" Command="{Binding NewDateSelectedCommand}" />
</Popup.InputBindings>

1 个答案:

答案 0 :(得分:0)

您可以,必须将Focus()设置为弹出窗口

<Popup x:Name="popup">
    <Popup.InputBindings>
       <KeyBinding Key="Escape" Command="Close" />
       <KeyBinding Key="Enter" Command="{Binding NewDateSelectedCommand}" />
    </Popup.InputBindings>
</Popup>

public void ShowPopup()
{
    this.popup.IsOpen = true;
    this.popup.Focus();
}