简体   繁体   中英

Set Textbox Focus within a Control Template

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

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

The border of the text box itself is made invisible.

My problem is this: when the custom control is "tabbed" to in the UI, the control gets KeyboardFocus , but the Textbox itself does not. This causes the blinking cursor not to show.

I need to know how to pass focus to the Textbox contained within the border, named DisplayText , from a trigger in the control template.

I tried using the FocusManager to set DisplayText to be the focused element, but that didn't work.

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

Control 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>

Try adding Focusable="False" to the Label .

I tried to copy your XAML into a Window and run it, but there's obviously a bunch of other stuff that I would need in order to get it to work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM