插入符号未显示在文本框中

时间:2014-09-17 04:47:19

标签: c# wpf textbox caret

我在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实现如下:

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

但我的问题是Caret从未显示在我的文本框中。 如果我使用没有我的风格的文本框我可以看到护理。所以它必须与我的风格有关。 我如何改变我的风格来展示插入符?

提前致谢!

1 个答案:

答案 0 :(得分:2)

将模板中的ContentPresenter更改为ScrollViewer,并将其命名为PART_ContentHost

变化

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

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

文字&amp;文本框中的插入符号呈现是通过PART_ContentHost

完成的

来自MSDN

  

PART_ContentHost :可以包含FrameworkElement的可视元素。 TextBox的文本显示在此元素中。