Set the focus of the text field in the control template

I have a control template that defines a custom floating text field.

It consists of a label, a border that serves as a visual border for the text box, and a text box inside that border.

The border of the text field itself becomes invisible.

My problem is this: when the user control is in the user interface, the control receives KeyboardFocus, but does Textboxnot. This causes the blinking cursor to not appear.

I need to know how to pass focus to Textbox, contained on a border, with a name DisplayText, from a trigger in a control template.

I tried using FocusManagerto set DisplayTextas a focused element, but that didn't work.

Any ideas, thoughts or advice would be greatly appreciated. If you need more information, please let me know.

Management Template:

<Grid SnapsToDevicePixels="True"
    UseLayoutRounding="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="floatingLabel"
               Template="{DynamicResource LabelControlTemplate1}"
               Content="{Binding LabelText, RelativeSource={RelativeSource Mode=TemplatedParent}}"
               IsHitTestVisible="False"
               Panel.ZIndex="2"
               Background="White"
               Height="15"
               VerticalContentAlignment="Center"
               Padding="3,0,3,0"
               HorizontalAlignment="Left"
               FontFamily="Segoe UI"
               FontSize="{Binding LabelFontSize, RelativeSource={RelativeSource TemplatedParent}}"
               Foreground="{DynamicResource FloatingLabelTextBox.Label.Foreground}"
               VerticalAlignment="Center">

        <Label.Tag>
            <sys:Double>0.0</sys:Double>
        </Label.Tag>

        <Label.Margin>
            <MultiBinding Converter="{StaticResource floatingLabelMarginConverter}">
                <Binding Path="Tag"
                             RelativeSource="{RelativeSource Self}" />
                <Binding ElementName="Border"
                             Path="ActualHeight" />
            </MultiBinding>
        </Label.Margin>
    </Label>

    <Border x:Name="Border"
                Height="{Binding TextBoxHeight, RelativeSource={RelativeSource TemplatedParent}}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Background="{TemplateBinding Background}"
                CornerRadius="3"
                SnapsToDevicePixels="True"
                Panel.ZIndex="0"
                VerticalAlignment="Bottom">

        <Grid x:Name="GridContainer" Width="{Binding ElementName=Border, Path=ActualWidth}" Margin="10,0,0,0">
            <TextBox x:Name="DisplayText" 
                     Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=FormattedPhoneNumber, StringFormat={}{0:(###)###-####}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                     VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                     FontFamily="{TemplateBinding FontFamily}"
                     FontSize="{TemplateBinding FontSize}"
                     FontWeight="{TemplateBinding FontWeight}"
                     Foreground="{TemplateBinding Foreground}"
                     Width="{Binding ElementName=Border, Path=ActualWidth}">
                <TextBox.Template>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <ScrollViewer x:Name="PART_ContentHost" 
                                      HorizontalAlignment="Stretch" 
                                      Margin="{TemplateBinding Padding}" 
                                      Uid="ScrollViewer_1" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>
        </Grid>
    </Border>
</Grid>

Trigger:

<Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=DisplayText}" />
                    </Trigger>
+4
source share
1 answer

Try adding Focusable="False"to Label.

I tried to copy your XAML to a window and run it, but there will obviously be a lot of other things that I will need to get it working.

+1
source

Source: https://habr.com/ru/post/1620815/


All Articles