I've defined a control template for my TextBox to achieve something like:
The code is simple:
<ControlTemplate TargetType="TextBox" x:Key="TextBoxTemplate">
<Border Name="Border" BorderBrush="blue" BorderThickness="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Name="Label"
Grid.Column="0"
VerticalContentAlignment="Center"
Width="18"
VerticalAlignment="Stretch"
Background="Blue"
Foreground ="white"
FontWeight="Bold"
FontSize="14"
Content=">"/>
<TextBox Name="InnerTextBox"
BorderBrush="{x:Null}" VerticalContentAlignment="Center" Grid.Column="1"
Text="{TemplateBinding Text}" HorizontalAlignment="Stretch"
/>
</Grid>
</Border>
</ControlTemplate>
Now I want that when a control IsFocused, the border and label change their color and become, say, red.
So I added the following:
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="Label" Property="Background" Value="Red"/>
<Setter TargetName="Border" Property="BorderBrush" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
Now, when I give focus to the controls with the tab key, the focused one correctly becomes red.
But if I click directly into the control, it doesn't.
It's like the focus of the inner TextBox is not considered as focus for the templated control, that sort of makes sense. I thought of setting it via Trigger, but it's a readonly property.
What's the neat solution for this?
Please notice that this question is about focus handling in a ControlTemplate, so it's different from this one.
A similar question
I've been suggested that there is a simliar question here Currently, all of those solutions but one are not based on a ControlTemplate. This question instead is focused on a working implementation of the most basic ControlTemplate for a TextBox, and in particular how to make the focus work properly. The visual similarity to that question comes from my effort to keep things simple, so I thought of a 'simply decorated text box'. The example could have been based on an enriched TextBox with controls to show validation errors in different ways and so on. The question here is how to make the focus work, not how the different ways to add hinttext or other decorations to a textbox.