When focusing a TextBox with Tab, the cursor does not show. This does not change when typing. This problem is only resolved when the user clicks inside the TextBox.
Here is the code for my 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" />
I tried to resolve the error by adding this.Cursor = Cursors.IBeam;
to the Change Text Event, but it did not work.
EDIT: I have reproduced this glitch in a new project with two TextBoxes in an empty window with the following style:
<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>
When clicking on the first TextBox and then pressing Tab to focus the second TextBox. The cursor will not be visible while typing.
A TextBox
style should include any childTextBox
elements. Try this:
<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>