繁体   English   中英

弹出框中的文本框在WPF应用程序中没有得到关注

[英]Textbox in popup doesn't get focus in WPF application

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

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

顶部有一个标题,一个文本框和两个按钮,分别是connect和cancel。 同样,没有什么复杂的。

所有这些工作。 您单击一个网络,我们将显示弹出窗口和键盘,但以下情况除外:密码文本框永远不会成为焦点。 即使您单击它。 没有重点。 我发现让它集中注意力的唯一技巧是单击弹出窗口(例如回到列表视图,如果弹出窗口已打开,它已经忽略了单击,因此很安全),然后单击文本框,然后瞧! 焦点。 我确实不希望将其放入用户手册中。

这是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 click事件中试图将焦点集中到控件上的操作。 请注意,我尝试伪造“将焦点设置到列表视图,然后将其设置到文本框,但这没有用。

// 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新手警报。

我看到了这篇文章 ,但是并没有太大帮助,因为我认为我已经做了所有这些事情。

代替MouseDown ,在ListView/ListViewItem上注册MouseUp事件。 在处理程序中,您可以执行

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

ListView上的MouseUp将焦点从Popup移开,因此请在MouseUp中打开弹出窗口而不是MouseDown

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM