【发布时间】: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}" />
这是页面加载时的样子:
上图没有显示文本光标/插入符号。我知道我的TextBox 是Focused,因为我可以输入:
这里的问题是,文本光标/插入符号仅在我单击 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