问题
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.
Here is my TextBox style:
<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.
回答1:
TextBox
template requires named part PART_ContentHost
so you'll need to replace 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
PART_ContentHost A visual element that can contain a FrameworkElement. The text of the TextBox is displayed in this element.
回答2:
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
.
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.
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.
来源:https://stackoverflow.com/questions/24204967/how-to-display-caret-in-custom-wpf-text-box