0

I Have this Style template

    <Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <TextBox Text="{Binding Path=Text,
                                            RelativeSource={RelativeSource TemplatedParent}, 
                                            Mode=TwoWay,
                                            UpdateSourceTrigger=PropertyChanged}"
                             x:Name="textSource" 
                             Background="Transparent" 
                             Focusable="True"
                             Panel.ZIndex="2">                            
                    </TextBox>
                    <TextBox Text="{TemplateBinding Tag}" Background="{TemplateBinding Background}" Panel.ZIndex="1">
                        <TextBox.Style>
                            <Style TargetType="{x:Type TextBox}">
                                <Setter Property="Foreground" Value="Transparent"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
                                        <Setter Property="Foreground" Value="LightGray"/>
                                    </DataTrigger>                                         
                                </Style.Triggers>
                            </Style>
                        </TextBox.Style>
                    </TextBox>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And In a Custom Control have a fiel that uses this template:

<TextBox x:Name="DatoABuscar" 
            Height="Auto" 
            MinHeight="25" 
            Text="{Binding Filtro,UpdateSourceTrigger=PropertyChanged}" 
            Margin="1,1,1,0" 
            PreviewKeyDown="DatoABuscar_PreviewKeyDown" 
            VerticalAlignment="Center"
            Style="{StaticResource placeHolder}"
            Tag="{x:Static resources:Labels.CONTROLES_SearchPlaceHoderText}"
            GotFocus="DatoABuscar_GotFocus"/>

As you cold see I have a GotFocus handler with this code:

private void DatoABuscar_GotFocus(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).SelectAll();        
}

This handle Works perfect and I pretend to select all the content of the textbox when GotFocus Happens, but If I understand the real textbox showed after GotFocus happen is textSource inner into the style template.

Now My Questions are?

  1. How can I do to propagate the got focus received by DatoAbuscar to textSource? or
  2. How can i do to Access the visualtree under the DatoABuscar to get textSource? or
  3. How can i do to Select all text in textSource when DatoAbuscar recieves the GotFocus**?

Another aproach ?

EDIT: I'm totally wrong the problem isn't the TexBox where I'm selecting the text. My problema was the method to select the text. Finally I Implement This Post

Community
  • 1
  • 1
Juan Pablo Gomez
  • 5,203
  • 11
  • 55
  • 101

1 Answers1

1

Actually you only need to use FindName to find the "textSource".

private void DatoABuscar_GotFocus(object sender, RoutedEventArgs e)
{
    var txt = sender as TextBox;
    var innerTxt = txt.Template.FindName("textSource", txt) as TextBox;
    Keyboard.Focus(innerTxt);
    innerTxt.SelectAll();
}
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67