当 TextBox 聚焦于 Tab 时光标不显示
Cursor Not Showing When TextBox is Focused With Tab
当使用 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;
添加到更改文本事件来解决错误,但没有成功。
编辑:我在一个新项目中重现了这个故障,在一个空 window 中有两个文本框,样式如下:
<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>
当单击第一个文本框然后按 Tab 以聚焦第二个文本框时。键入时光标将不可见。
TextBox
样式应包含任何子TextBox
元素。试试这个:
<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="{DynamicResource ForegroundColor}" />
<Setter Property="CaretBrush" Value="{DynamicResource ForegroundColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border"
Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Panel.ZIndex="2"
SnapsToDevicePixels="True">
<Grid>
<TextBlock Text="{TemplateBinding Tag}" Foreground="LightGray">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当使用 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;
添加到更改文本事件来解决错误,但没有成功。
编辑:我在一个新项目中重现了这个故障,在一个空 window 中有两个文本框,样式如下:
<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>
当单击第一个文本框然后按 Tab 以聚焦第二个文本框时。键入时光标将不可见。
TextBox
样式应包含任何子TextBox
元素。试试这个:
<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="{DynamicResource ForegroundColor}" />
<Setter Property="CaretBrush" Value="{DynamicResource ForegroundColor}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="border"
Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Panel.ZIndex="2"
SnapsToDevicePixels="True">
<Grid>
<TextBlock Text="{TemplateBinding Tag}" Foreground="LightGray">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<ScrollViewer x:Name="PART_ContentHost" Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" TargetName="border" Value="0.56"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>