Caret is not showing in the text box

I am using the following style for my textbox in WPF:

<Style x:Key="myTxtBoUserInputMedStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Height" Value="35"/>
    <Setter Property="Background" Value="Gainsboro"/>
    <Setter Property="BorderBrush" Value="LightGray"/>
    <Setter Property="BorderThickness" Value="0.5"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Padding" Value="2,2,2,2"/>
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="Background" Value="LightGray"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="CaretBrush" Value="Black"/>
    <EventSetter Event="KeyDown" Handler="TextBoxKeyDownHandler"/>
    <EventSetter Event="GotFocus" Handler="TextBoxGotFocusHandler"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        TextElement.Foreground="Black"
                        CornerRadius="2">
                   <ContentPresenter
                        Content="{TemplateBinding Text}"
                        Width="145"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center"
                        Height="24" Margin="5,0,0,0">
                    </ContentPresenter>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Background" Value="LightBlue"/>
                        <Setter Property="Foreground" Value="Black"/>
                        <Setter Property="FontWeight" Value="Bold"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

      

GotFocusEvent is implemented like this:

private void TextBoxGotFocusHandler(object sender, RoutedEventArgs e)
{
    TextBox tb = (TextBox)sender;
    if (tb.SelectedText.Length == 0)
    {
        tb.CaretIndex = tb.Text.Length;
    }
}

      

But my problem is that Caret never shows up in my textbox. If I use a textbox without my styling I can see the concern. So it should be related to my style. How do I change my style to show the caret?

Thanks in advance!

+3


source to share


1 answer


change ContentPresenter

in template to ScrollViewer

and give it a namePART_ContentHost

change

<ContentPresenter Content="{TemplateBinding Text}"
                  Width="145"
                  VerticalAlignment="Center"
                  HorizontalAlignment="Center"
                  Height="24"
                  Margin="5,0,0,0">
</ContentPresenter>

      

to



<ScrollViewer x:Name="PART_ContentHost"
              Width="145"
              VerticalAlignment="Center"
              HorizontalAlignment="Center"
              Height="24"
              Margin="5,0,0,0" />

      

the text and caret rendering in the textbox is done with PART_ContentHost

from MSDN

PART_ContentHost : A visual element that can contain a FrameworkElement. The TextBox text is displayed in this element.

+2


source







All Articles