【发布时间】:2019-03-19 02:06:06
【问题描述】:
当使用 Tab 聚焦文本框时,光标不显示。这在键入时不会改变。只有当用户在 TextBox 内单击时,才能解决此问题。
这是我的文本框的代码:
<TextBox x:Name="NameField"
Style="{StaticResource placeHolder}"
Tag="Name"
FontFamily="Courier New"
FontSize="48"
VerticalAlignment="Top"
FontWeight="Bold"
BorderBrush="{x:Null}"
SelectionBrush="{DynamicResource AccentColor}"
BorderThickness="0"
TextChanged="ChangeName"
Height="49"
KeyDown="UndoRedoKeyPress" />
我尝试通过将this.Cursor = Cursors.IBeam; 添加到更改文本事件来解决错误,但没有成功。
编辑:我在一个新项目中重现了这个故障,在一个空窗口中有两个文本框,样式如下:
<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource"
Background="Transparent"
Foreground="{DynamicResource ForegroundColor}"
CaretBrush="{DynamicResource ForegroundColor}"
BorderBrush="{TemplateBinding BorderBrush}"
SelectionBrush="{TemplateBinding SelectionBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Panel.ZIndex="2"
AcceptsReturn="{TemplateBinding AcceptsReturn}"
AcceptsTab="{TemplateBinding AcceptsTab}"
TextWrapping="{TemplateBinding TextWrapping}" />
<TextBox Text="{TemplateBinding Tag}" Background="{DynamicResource BackgroundColor}" Panel.ZIndex="1" BorderBrush="{TemplateBinding BorderBrush}"
SelectionBrush="{TemplateBinding SelectionBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
单击第一个 TextBox 然后按 Tab 聚焦第二个 TextBox。键入时光标将不可见。
【问题讨论】:
-
根据您提供的信息无法复制您的问题。请阅读this 并修改您的问题以包含您的问题的 MCVE。
-
@mm8 我已经更新了我的原始帖子,提供了更多详细信息。