That's because ListHeaderTemplate
returns the datatemplate. A datatemplate is, as its name indicate, a template: a blueprint indicating what controls should be built and how. It's not the actual control.
I don't know if the instantiated template is accessible in the LongListSelector. However, you can get around the problem by using ListHeader
instead:
<phone:LongListSelector ItemsSource="{Binding ListModel}" x:Name="longListSelector"
IsGroupingEnabled="True" LayoutMode="List" HideEmptyGroups="False"
ItemTemplate="{StaticResource ItemTemplate}">
<phone:LongListSelector.ListHeader>
<Border Background="#D62429" Opacity="0.8" Height="50">
<TextBlock Name="txtHeader" Text="Long List Header" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" FontFamily="Segoe UI Semilight"/>
</Border>
</phone:LongListSelector.ListHeader>
</phone:LongListSelector>
Then you can call your method by using the same ListHeader
property:
FindFirstElementInVisualTree<TextBlock>((FrameworkElement)this.longListSelector.ListHeader);
Or even by using directly the textbox's name:
this.txtHeader.Text = "Hello world!";