I am looking for a way to show the Caret in a TextBox
when IsMouseOver="True"
. The solution using FocusManager.IsFocusScope="True"
as mentioned here does not seem to work.
Is there a way to show the Caret without having to click inside the TextBox
?
MWE:
<Window x:Class="WpfApp11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FocusManager.IsFocusScope" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox Width="300" Height="22" Text="Hello World" />
</Grid>
</Window>
A TextBox
displaying the Caret after clicking on it. I want the Caret to show already on mouse over.
If you want to set focus, then you need to use FocusedElement
( credits ):
<Style BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="FocusManager.FocusedElement"
Value="{Binding RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>
If you want caret to be initially at the end then consider to initialize all textboxes like this ( credits ):
<TextBox CaretIndex="{x:Static system:Int32.MaxValue}" ... />
where system
is defined like this:
<Window xmlns:system="clr-namespace:System;assembly=mscorlib" ... />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.