【发布时间】:2016-08-29 21:08:08
【问题描述】:
我的 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 占据了焦点。知道如何模糊它吗?
我现在有一个解决方法:如果我通过聚焦另一个文本框来模糊文本框,那么弹出窗口会接受焦点。
但是,如果用户单击 txtTypeEditor 文本框,然后单击弹出窗口中的输入字段,则鼠标焦点会切换到弹出窗口,但键盘焦点仍保留在文本框上。
【问题讨论】: