3
我想從Popup控件接收鍵盤輸入,該控件充當觸摸屏鍵盤控件的根視覺。我希望控件支持鍵盤輸入以及觸摸屏輸入。我連接事件(PreviewKeyDown和KeyDown)並且它們從不被解僱。如何從Popup控件捕獲鍵盤輸入
我想從Popup控件接收鍵盤輸入,該控件充當觸摸屏鍵盤控件的根視覺。我希望控件支持鍵盤輸入以及觸摸屏輸入。我連接事件(PreviewKeyDown和KeyDown)並且它們從不被解僱。如何從Popup控件捕獲鍵盤輸入
A Popup
默認情況下是不可聚焦的,即使它是可聚焦的,您還必須在彈出窗口上有其他可聚焦的內容,然後讓它聚焦或將其聚焦以便接收鍵盤事件。
換句話說,如果你想從Popup
使用Focusable="True"
鍵盤事件,把一個可聚焦控制像TextBox
或Button
或ListBox
,然後讓用戶通過點擊或手動使用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>
不幸的是給定的要求,不允許一個文本框。該界面是需要響應鍵盤輸入的觸摸屏鍵盤(即,如果用戶點擊鍵盤上的按鍵被擊中的'E'鍵)。當用戶點擊或觸摸否則保持只讀狀態的文本字段(即TextBlock)時,彈出鍵盤。 – Jordan 2011-01-26 14:59:07