如何在自定义WPF文本框中显示插入符号

时间:2023-01-28 12:10:47

I have designed my own text box in WPF by creating my own custom control template. However, I cannot seem to get the caret to show up.

我通过创建自己的自定义控件模板在WPF中设计了自己的文本框。但是,我似乎无法让插入符号显示出来。

Here is my TextBox style:

这是我的TextBox样式:

<Style TargetType="{x:Type TextBox}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="True"/>
        <Setter Property="TextBlock.Foreground" Value="DeepSkyBlue"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border
                        Name="border"
                        CornerRadius="0"
                        Padding="2"
                        BorderThickness="1"
                        BorderBrush="DeepSkyBlue">
                        <ContentPresenter
                            HorizontalAlignment="Left" 
                            VerticalAlignment="Top"
                            Content="{TemplateBinding Text}"
                            Name="content"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="True">
                            <Setter Property="Background" Value="MidnightBlue"/>
                            <Setter Property="BorderBrush" Value="DeepSkyBlue"/>
                            <Setter Property="Foreground" Value="DeepSkyBlue"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

So how can I get my caret to show up? I want it to be MidnightBlue so it is visible over the DeepSkyBlue background.

那么我怎样才能让我的插图显示出来?我希望它是MidnightBlue所以它在DeepSkyBlue背景上可见。

2 个解决方案

#1


2  

TextBox template requires named part PART_ContentHost so you'll need to replace ContentPresenter

TextBox模板需要命名部分PART_ContentHost,因此您需要替换ContentPresenter

<ControlTemplate TargetType="{x:Type TextBox}">
   <Border Name="border" CornerRadius="0" Padding="2" BorderThickness="1" BorderBrush="DeepSkyBlue">
      <ScrollViewer x:Name="PART_ContentHost"/>
   </Border>
   <ControlTemplate.Triggers>
      <Trigger Property="IsEnabled" Value="True">
         <Setter Property="Background" Value="MidnightBlue"/>
         <Setter Property="BorderBrush" Value="DeepSkyBlue"/>
         <Setter Property="Foreground" Value="DeepSkyBlue"/>
      </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

TextBox Styles and Templates

TextBox样式和模板

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

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

#2


1  

While the question author has been provided with a solution to their problem, this solution may not address other users' problem. One other reason that can cause that the caret in a TextBox to disappear is when you have set the TextBox.Background property to {x:Null} or to Transparent.

虽然问题作者已经为他们的问题提供了解决方案,但该解决方案可能无法解决其他用户的问题。另一个可能导致TextBox中的插入符号消失的原因是当您将TextBox.Background属性设置为{x:Null}或Transparent时。

This happens because the WPF Framework sets the colour of the caret to be opposite to the colour set in the Background property. As these two values have no opposite, the caret colour is set to Black. The solution in these cases is very simple.

发生这种情况是因为WPF框架将插入符的颜色设置为与Background属性中设置的颜色相反。由于这两个值没有相反的值,因此插入符颜色设置为黑色。这些情况下的解决方案非常简单。

We can set the colour of the Background property to the opposite colour that we want the caret to be, but then set the alpha values of the declared colour to 00. For example, if you want a white caret, then set the TextBox.Background property to #00000000 (transparent black) and if you want a red caret, then set the TextBox.Background property to #0000FFFF (transparent cyan), etc.

我们可以将Background属性的颜色设置为我们想要插入符号的相反颜色,但是然后将声明颜色的alpha值设置为00.例如,如果你想要一个白色插入符号,那么设置TextBox.Background属性为#00000000(透明黑色),如果你想要一个红色插入符号,则将TextBox.Background属性设置为#0000FFFF(透明青色)等。

#1


2  

TextBox template requires named part PART_ContentHost so you'll need to replace ContentPresenter

TextBox模板需要命名部分PART_ContentHost,因此您需要替换ContentPresenter

<ControlTemplate TargetType="{x:Type TextBox}">
   <Border Name="border" CornerRadius="0" Padding="2" BorderThickness="1" BorderBrush="DeepSkyBlue">
      <ScrollViewer x:Name="PART_ContentHost"/>
   </Border>
   <ControlTemplate.Triggers>
      <Trigger Property="IsEnabled" Value="True">
         <Setter Property="Background" Value="MidnightBlue"/>
         <Setter Property="BorderBrush" Value="DeepSkyBlue"/>
         <Setter Property="Foreground" Value="DeepSkyBlue"/>
      </Trigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

TextBox Styles and Templates

TextBox样式和模板

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

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

#2


1  

While the question author has been provided with a solution to their problem, this solution may not address other users' problem. One other reason that can cause that the caret in a TextBox to disappear is when you have set the TextBox.Background property to {x:Null} or to Transparent.

虽然问题作者已经为他们的问题提供了解决方案,但该解决方案可能无法解决其他用户的问题。另一个可能导致TextBox中的插入符号消失的原因是当您将TextBox.Background属性设置为{x:Null}或Transparent时。

This happens because the WPF Framework sets the colour of the caret to be opposite to the colour set in the Background property. As these two values have no opposite, the caret colour is set to Black. The solution in these cases is very simple.

发生这种情况是因为WPF框架将插入符的颜色设置为与Background属性中设置的颜色相反。由于这两个值没有相反的值,因此插入符颜色设置为黑色。这些情况下的解决方案非常简单。

We can set the colour of the Background property to the opposite colour that we want the caret to be, but then set the alpha values of the declared colour to 00. For example, if you want a white caret, then set the TextBox.Background property to #00000000 (transparent black) and if you want a red caret, then set the TextBox.Background property to #0000FFFF (transparent cyan), etc.

我们可以将Background属性的颜色设置为我们想要插入符号的相反颜色,但是然后将声明颜色的alpha值设置为00.例如,如果你想要一个白色插入符号,那么设置TextBox.Background属性为#00000000(透明黑色),如果你想要一个红色插入符号,则将TextBox.Background属性设置为#0000FFFF(透明青色)等。