Stephan Jennewein
Stephan Jennewein

Reputation: 313

Hand focus to inner element in XAML

I have a custom element which looks like:

    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Grid>
                    <TextBox Name="textBox"
                         Grid.ZIndex="1"
                         Padding="0,3,0,0"
                         Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Label}}, Path=Content, UpdateSourceTrigger=PropertyChanged}"
                         Opacity="0"
                         IsEnabled="False"
                         Focusable="True"
                         />


                    <Border Name="boxBorder"  BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" Padding="3,0,0,0" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </Grid>

I want a label which you can double click and then turns into a input field (textbox). Therefore I have defined a double click event with:

    <EventTrigger RoutedEvent="MouseDoubleClick">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="textBox"
                                                 Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                <BooleanAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="textBox"
                                                                Storyboard.TargetProperty="IsEnabled">
                                    <DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
                                </BooleanAnimationUsingKeyFrames>

                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>

The double clicking works fine, but you have to perform a triple click to get the focus into the textbox. Just double clicking turns only the opacity to 1. I have not found a way how I can move the focus with the double click event to the textbox.

Upvotes: 2

Views: 654

Answers (2)

D J
D J

Reputation: 7028

You can implement an attached behavior of text box which set the focus on textbox when it is enable or visible.

Upvotes: 0

Fede
Fede

Reputation: 44038

I think this could help. It has some code involved, but it is a reusable and scalable solution using WPF behaviors.

Upvotes: 1

Related Questions