0

I've defined a control template for my TextBox to achieve something like:

My starting control template

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.

Correct behavior

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.

AgostinoX
  • 7,477
  • 20
  • 77
  • 137
  • 2
    There shouldn't be a TextBox in the Template of a TextBox. See here: https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/textbox-styles-and-templates. It should have `` – Clemens Aug 18 '20 at 09:49
  • Thanks @Clemens, this is the solution. Unfortunately they close my question, solely relying on an aesthetic resemblance of the results. I bet that if i didn't include the screenshot the question wouldn't be considered equal. I leave it for a while to see if they reopen it and you provide the solution as an answer, I would accept it. Otherwise i will delete the question or provide the answer inside it. – AgostinoX Aug 18 '20 at 10:21
  • I also voted to close your question, since it was asking for the wrong thing in the first place. The marked original question clearly shows how to modify the visual appearance of a TextBox by replacing its ControlTemplate. Editing a solution into the question is not appropriate. – Clemens Aug 18 '20 at 10:26
  • What is the wrong thing? I show a malfunctioning caused by a design error that could happen to anybody. The focus problem showed a bigger problem of not having used the right implemetation, that may be helpful for other people making the same error. – AgostinoX Aug 18 '20 at 10:28
  • A TextBox in the ControlTemplate of a TextBox is obviously wrong. – Clemens Aug 18 '20 at 10:30
  • That's what I said, If there were no people doing mistakes probably there won't be 80% of stackoverflow, don't you think? If questions that are errors get closed, what should people ask? – AgostinoX Aug 18 '20 at 11:24
  • @Clemens, they reopened the question. If you want to provide your first comment as the solution I would accept it, otherwise I will do it myself after a while. – AgostinoX Oct 06 '20 at 21:20

0 Answers0