弹出窗口中的文本框无法在WPF应用程序中获得焦点

时间:2013-10-22 02:41:19

标签: c# wpf textbox popup focus

我正在为自助服务终端应用程序构建一个窗口,该应用程序具有连接到wifi网络的管理屏幕。实际上,我正在移植已经执行此操作的现有WinForms应用程序,但是没有为我们提供创建更有趣UI的灵活性。所以,我们正在转向WPF。

窗口非常简单,它有一个列表视图来显示它找到的网络,如果你点击它,它将连接到它。为了连接,我们需要提示该网络的安全代码(如果需要)。为此,我们打开一个弹出窗口,其中有三个部分 - 顶部的“dialog-y”提示符部分,一个间隔行和一个空白边框,它将位于屏幕键盘后面,但有圆角。

该顶部有一个标题,一个文本框和两个按钮,连接和取消。再一次,没有什么复杂的。

这一切都有效。您单击一个网络,我们会显示弹出窗口和键盘,但以下情况除外:密码的文本框永远不会成为焦点。即使你点击它。没有焦点。我发现让它专注的唯一技巧是点击弹出窗口(就像回到列表视图一样,如果弹出窗口打开已经忽略了点击,那么它是安全的),然后点击文本框,然后点击!焦点。我真的不认为我想把它放在用户手册中。

这是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>

以下是我在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);

我为tbPasscode的DependencyElement尝试了几个不同的东西,但我真的不知道我在做什么,或者我正在做的是有所作为。哦,我提到我刚完成WPF编码的第一周了吗?是的,WPF新手警报。

我看到了this post,但它并没有多大帮助,因为我以为我已经做了这一切。

1 个答案:

答案 0 :(得分:0)

而不是MouseDown,请在MouseUp上注册ListView/ListViewItem个活动。 在处理程序中,你可以做

     popPasscode.IsOpen = true;
     Keyboard.Focus(tbPasscode);

MouseUp上的ListView将注意力从Popup上移开,因此请在MouseUp中打开Popup而不是MouseDown