Cursor not blinking in templated TextBox
Asked Answered
B

1

7

I have the following TextBox in one of my views:

<TextBox Name="SearchTerm" Style="{StaticResource SearchTermTextBoxStyle}"
         Text="{Binding TemplatesViewModel.SearchTerm, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"
         attachedProperties:Watermark.Watermark="Some watermark text"
         KeyboardNavigation.TabIndex="5" />

Which has, as you can see, the following style applied:

<Style x:Key="SearchTermTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <TextBox Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" />
                    <TextBlock Text="{Binding (attachedProperties:Watermark.Watermark), Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" x:Name="DefaultTextPrompt"
                           Foreground="#888888" FontStyle="Italic" HorizontalAlignment="Left"
                           VerticalAlignment="Center" IsHitTestVisible="False" Visibility="Hidden"
                           Margin="5,-1,0,0" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Text" Value="" />
                            <Condition Property="IsKeyboardFocusWithin" Value="False" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility" TargetName="DefaultTextPrompt" Value="Visible" />
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The problem is that when I obtain a focus on the SearchTerm TextBox, the cursor will not appear at all. First that came to my mind was simple conclusion: probably I get logical focus only, not keyboard focus. But that is not true. I can freely type in some text into that TextBox and I am still not getting cursor. When I am entering this TextBox using left mouse button, cursor appears and is blinking as expected.
What is wrong with my TextBox then?

Buckwheat answered 20/1, 2017 at 10:51 Comment(0)
T
0

The problem lies on the TextBox control that you put inside the TextBox's ControlTemplate. I suppose when you focus on the (parent) TextBox, it does not focus on the ControlTemplate's TextBox.

When you use left click, it just goes directly to the ControlTemplate's TextBox, so you have blinking cursor.

If you notice in the original (default) TextBox's ControlTemplate, it uses ScrollViewer to host the content, so I would recommend you to change

<TextBox Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" />

to

<ScrollViewer x:Name="PART_ContentHost" Focusable="False" />

This will automagically show the Text from the TemplatedParent. And also let you focus on the (parent) TextBox.

Teishateixeira answered 8/8, 2018 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.