2011-01-25 95 views
3

我想从Popup控件接收键盘输入,该控件充当触摸屏键盘控件的根视觉。我希望控件支持键盘输入以及触摸屏输入。我连接事件(PreviewKeyDown和KeyDown)并且它们从不被解雇。如何从Popup控件捕获键盘输入

回答

1

A Popup默认情况下是不可聚焦的,即使它是可聚焦的,您还必须在弹出窗口上有其他可聚焦的内容,然后让它聚焦或将其聚焦以便接收键盘事件。

换句话说,如果你想从Popup使用Focusable="True"键盘事件,把一个可聚焦控制像TextBoxButtonListBox,然后让用户通过点击或手动使用Focus()从给它焦点码。如果你做了所有这些事情,则PreviewKeyDown应该为Popup启动。

这里是打开一个Popup一个切换按钮,一个小演示程序,并显示一个滑块增加每当我们得到一个Popup一个PreviewKeyDown事件:

<Grid> 
    <StackPanel> 
     <Slider Name="slider1"/> 
     <ToggleButton x:Name="toggleButton1" 
         Content="Open Popup"/> 
    </StackPanel> 
    <Popup PlacementTarget="{Binding ElementName=toggleButton1}" 
      IsOpen="{Binding IsChecked, ElementName=toggleButton1, Mode=OneWay}" 
      Focusable="True"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="PreviewKeyDown"> 
       <ei:ChangePropertyAction TargetObject="{Binding ElementName=slider1}" 
             PropertyName="Value" 
             Value="1" 
             Increment="True"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
     <TextBox Background="White" 
       Focusable="True"> 
      <TextBox.Text>Sample Popup content.</TextBox.Text> 
     </TextBox> 
    </Popup> 
</Grid> 
+0

不幸的是给定的要求,不允许一个文本框。该界面是需要响应键盘输入的触摸屏键盘(即,如果用户点击键盘上的按键被击中的'E'键)。当用户点击或触摸否则保持只读状态的文本字段(即TextBlock)时,弹出键盘。 – Jordan 2011-01-26 14:59:07