c# - 弹出窗口中的文本框未在 WPF 应用程序中获得焦点

标签 c# wpf textbox popup focus

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

这个窗口非常简单,它有一个 ListView 来显示它找到的网络,如果你点击一个,它就会连接到它。为了连接,我们需要提示输入该网络的安全代码(如果需要的话)。为此,我们打开一个包含三个部分的弹出窗口 - 顶部的“dialog-y”提示部分、间隔行和位于屏幕键盘后面但具有漂亮圆角的空白边框。

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

这一切都有效。您单击一个网络,我们会显示弹出窗口和键盘,除了:密码文本框永远不会获得焦点。即使你点击它。没有焦点。我发现让它集中注意力的唯一技巧是点击弹出窗口(比如返回 ListView ,如果弹出窗口打开,它已经忽略点击,所以它是安全的),然后点击文本框,瞧!焦点。不过,我真的不认为我想把它放在用户手册中。

这是 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 单击事件期间尝试执行的操作,以使控件获得焦点。请注意,我试图伪造“将焦点设置到 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 ,但这并没有多大帮助,因为我认为我已经在做所有这些事情了。

最佳答案

代替 MouseDown,注册到 ListView/ListViewItem 上的 MouseUp 事件。 在处理程序中你可以做

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

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

关于c# - 弹出窗口中的文本框未在 WPF 应用程序中获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508126/

相关文章:

c# - 在 WPF 中继承样式

WPF关闭子关闭父窗口

WPF : TextBox binding not working after using DataTemplateSelector/ContentTemplateSelector

c# - 在 WPF 文本框中粘贴事件

c# - 再次单击 TextBox 使其散焦

c# - Entity Framework Code First 中的外键帮助

c# - Entity Framework 5 更新对于每个对象/行仅有效一次

c# - Canvas 中精确的像素移动

c# - C#中如何检查字符串的最后一个字符?

wpf - 有没有办法让 GridSplitter 不将元素推出窗口?