我的WPF应用程序中有几个弹出窗口,工作正常。
但是,最后一个不接受文本框中的焦点或输入。而且我不明白为什么。
<Label x:Name="lblSearch" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,5" Height="30" Width="Auto">Search</Label>
<TextBox Name="txtSearch" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,5" Height="30" Width="200" IsReadOnly="False" IsEnabled="True"></TextBox>
<Button Name="btnSearch" Grid.Row="0" Grid.Column="2" Style="{StaticResource ButtonSmall}" Template="{DynamicResource BlackButton}" Margin="5">Search</Button>
<Label x:Name="lblReplace" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,5" Height="30" Width="Auto">Replace</Label>
<TextBox Name="txtReplace" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,5" Height="30" Width="200" IsReadOnly="False" IsEnabled="True"></TextBox>
<Button Name="btnReplace" Grid.Row="1" Grid.Column="2" Style="{StaticResource ButtonSmall}" Template="{DynamicResource BlackButton}" Margin="5">Replace</Button>
<Button Name="btnStopSearch" Grid.Row="2" Grid.Column="3" Style="{StaticResource ButtonSmall}" Template="{DynamicResource BlackButton}" Margin="5">Done</Button>
<DockPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
</DockPanel>
</local:GridEx>
</DockPanel>
</Border>
</Border>
</Popup>
我尝试使用此代码强制关注它,但无济于事:
Private searchWhere As String = Nothing
Private Sub txtTypeEditor_keyDown(sender As Object, e As Forms.KeyEventArgs) Handles txtTypeEditor.KeyDown
If e.KeyCode = Forms.Keys.F AndAlso e.Control AndAlso Not e.Alt AndAlso Not e.Shift Then
PopupSearch.IsOpen = True
txtSearch.Focus()
Keyboard.Focus(txtSearch)
searchWhere = "TypeEditor"
e.Handled = True
End If
End Sub
Private Sub Popup1_Opened(sender As Object, e As EventArgs) Handles PopupSearch.Opened
txtSearch.Focus()
Keyboard.Focus(txtSearch)
End Sub
看来txtTypeEditor保持焦点。知道如何模糊吗?
我现在有一个解决方法:如果我通过聚焦另一个textBox来模糊文本框,那么弹出窗口接受焦点。
但是,如果用户单击txtTypeEditor文本框然后单击弹出窗口中的输入字段,则鼠标焦点会切换到弹出窗口,但键盘焦点会保留在文本框中。
答案 0 :(得分:0)
这只是一个猜测:
它可能与txtTypeEditor_keyDown
事件在txtTypeEditor中以某种方式保留有关吗?
您可以尝试使用keyPress
(如果它不影响代码的任何其他部分)