【问题标题】:How to display the text cursor/caret when a textbox is focused in wpf?当文本框聚焦于 wpf 时,如何显示文本光标/插入符号?
【发布时间】:2021-08-17 20:34:32
【问题描述】:

明确一点:当我的SignupWindow 加载时,我的TextBox已经专注。我的问题是除非我点击我的文本框,否则文本光标/插入符号不会显示(我认为这将是一个 UI 问题)。


我已经将我的 TextBox 集中在 Window 级别:

<Window x:Name="this"
        x:Class="Project.MVVM.Views.Windows.SignupWindow"
        <!-- some namespaces and properties here-->
        FocusManager.FocusedElement="{Binding ElementName=UsernameTextBox}">

我的TextBox

<TextBox x:Name="UsernameTextBox"
            Grid.Row="2"
            Width="303"
            Height="38"
            Text="{Binding Path=CurrentAccount.Username}"
            Style="{StaticResource TextBoxTheme}" />

这是页面加载时的样子:

上图没有显示文本光标/插入符号。我知道我的TextBoxFocused,因为我可以输入:


这里的问题是,文本光标/插入符号仅在我单击 TextBox 时显示:


这是我设置文本框的样式(如果这与问题相关):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}"
           x:Key="TextBoxTheme">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border CornerRadius="12"
                            Background="#2E3135"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}">
                        <TextBox Text="{Binding Path=Text,
                                                RelativeSource={RelativeSource Mode=TemplatedParent},
                                                UpdateSourceTrigger=PropertyChanged,
                                                Mode=TwoWay}"
                                 BorderThickness="0"
                                 VerticalContentAlignment="Center"
                                 Padding="20,0,20,0"
                                 Background="Transparent"
                                 Foreground="#B8BDC1"
                                 CaretBrush="#B8BDC1" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

【问题讨论】:

  • 如何在 window_loaded 事件上从代码后面设置焦点?试一试,让我们知道。
  • @GK 它聚焦,但不幸的是,文本光标/插入符号仍然不显示...

标签: c# wpf user-interface textbox cursor


【解决方案1】:

您在TextBox 中定义了TextBox,我猜您希望在显示窗口时内部TextBox 被聚焦,但实际上,外部TextBox 被聚焦。

我认为嵌套TextBox 不是不可能,而是麻烦的根源。为什么不定义一个正统但外观相同的Style?

<Style x:Key="TextBoxTheme" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="#B8BDC1"/>
    <Setter Property="CaretBrush" Value="#B8BDC1"/>
    <Setter Property="Padding" Value="20,0,20,0"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border CornerRadius="12"
                        Background="#2E3135">
                    <ScrollViewer x:Name="PART_ContentHost"
                                  Focusable="false"
                                  HorizontalScrollBarVisibility="Hidden"
                                  VerticalScrollBarVisibility="Hidden"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

  • 好吧,我现在无法使用该代码单击我的文本框。它不再专注。 (即使我删除了Focusable=false
  • 我不知道,因为您没有提供最小可重现示例。
  • 现在可以了。我刚刚删除了x:Name="PART_ContentHost",因为我认为命名它并不重要,这是问题所在。很抱歉让您感到困惑......
  • 没关系。通常在 WPF 中,ControlTemplate 中名称以PART_ 开头的元素对于该控件的工作是必不可少的,不得删除此类元素。
  • 感谢您提供更多信息 :))
【解决方案2】:

尝试在触发器/事件中的这些属性中添加 c# 代码(如果您需要确切的代码,请告诉我): image

【讨论】:

  • 我在MouseEnter 事件中添加了UsernameTextBox.Focus(),它专注于文本框,但文本光标/插入符号仍然不可见(除非我单击文本框以明确关注它)
  • 嗯,很有趣。尝试任何其他事件
猜你喜欢
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多