弹出窗口中的文本框在 WPF 应用程序中没有获得焦点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19508126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Textbox in popup doesn't get focus in WPF application
提问by gbryant
I'm building a window for a kiosk application that has an admin screen to connect to a wifi network. Actually, I'm porting an existing WinForms app that already does this, but doesn't give us the flexibility we want to create a more interesting UI. So, we're moving to WPF.
我正在为 kiosk 应用程序构建一个窗口,该窗口具有连接到 wifi 网络的管理屏幕。实际上,我正在移植一个已经这样做的现有 WinForms 应用程序,但没有给我们想要创建更有趣的 UI 的灵活性。所以,我们正在转向 WPF。
The window is pretty straightforward, it has a listview to show networks that it finds, and if you click on one, it will connect to it. In order to connect, we need to prompt for the security code for that network if it needs one. To do this, we open a popup that has three sections - a "dialog-y" prompt section at the top, a spacer row, and a blank border that will sit behind an onscreen keyboard, but have nice rounded corners.
这个窗口非常简单,它有一个列表视图来显示它找到的网络,如果你点击一个,它就会连接到它。为了连接,我们需要提示输入该网络的安全代码(如果需要)。为此,我们打开一个包含三个部分的弹出窗口 - 顶部的“dialog-y”提示部分、间隔行和位于屏幕键盘后面的空白边框,但具有漂亮的圆角。
That top section has a header, a text box, and two buttons, connect and cancel. Again, nothing complex.
顶部有一个标题、一个文本框和两个按钮,连接和取消。再次,没有什么复杂的。
All this works. You click a network, we show the popup and the keyboard, except: the textbox for the passcode never gets the focus. Even if you click on it. No focus. The only trick I've found to get it to focus is to click off the popup (like back on the listview, which is already ignoring clicks if the popup is open, so it's safe), then click back on the textbox, and voila! focus. I really don't think I want to put that in a user manual though.
所有这些都有效。你点击一个网络,我们会显示弹出窗口和键盘,除了:密码的文本框永远不会获得焦点。即使你点击它。没有重点。我发现让它聚焦的唯一技巧是点击弹出窗口(就像回到列表视图,如果弹出窗口打开,它已经忽略点击,所以它是安全的),然后点击文本框,瞧!重点。我真的不认为我想把它放在用户手册中。
Here's the popup portion of the xaml:
这是 xaml 的弹出部分:
<Popup x:Name="popPasscode" Placement="Top" HorizontalOffset="50" VerticalOffset="1000" AllowsTransparency="True" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="White" CornerRadius="20" Width="600" Height="400">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="#464646" Height="50" Margin="8,10,8,0" CornerRadius="25" >
<Label x:Name="lblTitleSecurityCode" Content="Enter the security code" Foreground="White" FontFamily="Arial" FontSize="30" FontWeight="Bold" HorizontalAlignment="Center"/>
</Border>
<TextBox Grid.Row="1" x:Name="tbPasscode" Height="50" FontFamily="Arial" FontSize="30" Margin="40,0,40,0"/>
<StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,0,10,10" HorizontalAlignment="Center">
<Controls:ImageButton x:Name="btnCodeConnect" Content="Connect" Height="70" Width="275" Foreground="Black" Style="{DynamicResource PlainButton}" FontFamily="Arial" FontSize="30" FontWeight="Bold" Click="btnCodeConnect_Click" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Controls:ImageButton x:Name="btnCodeCancel" Content="Cancel" Height="70" Width="275" Foreground="Black" Style="{DynamicResource PlainButton}" FontFamily="Arial" FontSize="30" FontWeight="Bold" Click="btnCodeCancel_Click" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Border>
<Border Grid.Row="2" x:Name="brdrKbd" Background="White" CornerRadius="20" Width="1200" Height="420"/>
</Grid>
</Popup>
Here's what I'm currently trying to do during the listview click event to get focus to the control. Note that I tried to fake the "set focus to the listview, then set it to the textbox, but that didn't work.
这是我目前在 listview 单击事件期间尝试执行的操作,以将焦点转移到控件上。请注意,我试图伪造“将焦点设置为列表视图,然后将其设置为文本框,但这不起作用。
// set the popup location and width and keyboard border width based on the current screen width
popPasscode.IsOpen = true;
// open the on-screen keyboard - synchronous call, doesn't return until it's open and idle
FocusManager.SetFocusedElement(this, lvAvailNetworks);
tbPasscode.Focusable = true;
FocusManager.SetFocusedElement(popPasscode, tbPasscode);
I've tried a couple different things for the DependencyElement for tbPasscode, but I really have no idea what I'm doing, or that what I'm doing is making any difference. Oh, did I mention I just finished my first week of WPF coding? Yup, WPF newbie alert.
我已经为 tbPasscode 的 DependencyElement 尝试了几种不同的方法,但我真的不知道我在做什么,或者我正在做的事情有什么不同。哦,我有没有提到我刚刚完成了 WPF 编码的第一周?是的,WPF 新手警报。
I saw this post, but it didn't help much, since I thought I was already doing all that.
我看到了这篇文章,但它没有多大帮助,因为我以为我已经在做所有这些了。
采纳答案by Nitin
Instead of MouseDown, register to MouseUpevent on ListView/ListViewItem.
In the handler you can do
而不是MouseDown注册到MouseUp事件 on ListView/ListViewItem。在处理程序中,您可以执行
popPasscode.IsOpen = true;
Keyboard.Focus(tbPasscode);
The MouseUpon your ListViewtakes focus away from the Popup, so open your Popup in MouseUp instead of MouseDown
在MouseUp您ListView需要从焦点离开Popup,所以在的MouseUp打开弹出窗口,而不是的MouseDown

