2

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.

Stephan Jennewein
  • 303
  • 1
  • 2
  • 10
  • possible duplicate of [WPF - Set Focus when a button is clicked - No Code Behind](http://stackoverflow.com/questions/2204063/wpf-set-focus-when-a-button-is-clicked-no-code-behind) – Federico Berasategui Oct 30 '13 at 22:30

2 Answers2

1

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

Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

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

D J
  • 6,908
  • 13
  • 43
  • 75