0

I'm targeting Windows Phone 8 and I would like to access my StackPanel named ListHeaderStackPanel which resides in a LongListSelector DataTemplate. My code looks as follows

<DataTemplate x:Key="ListHeader">
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,10,0,0">
        <StackPanel x:Name="ListHeaderStackPanel" HorizontalAlignment="Left" Height="8" VerticalAlignment="Top" Width="8">
        </StackPanel>
    </Grid>
</DataTemplate>

Is this possible? I've tried using FindName without luck and simply using ListHeaderStackPanel from code will not work.

Kara
  • 6,115
  • 16
  • 50
  • 57
Markus Persson
  • 1,103
  • 9
  • 21
  • 1
    You can try using the `VisualTreeHelper` class to walk the Visual Tree. There's a generic implementation here that may help http://stackoverflow.com/a/5191681/187697 – keyboardP Apr 23 '13 at 19:59
  • I've tried it by passing in my LongListSelector and LongListSelector.ListHeaderTemplate, both of them, unfortunately, returned null for StackPanel item. – Markus Persson Apr 23 '13 at 20:25
  • I just tried it and it's working for me: `HelperClass.FindDescendant(lls.ListHeaderTemplate.LoadContent());` Don't forget, it's generally a good idea to try and take advantage of DataBinding when possible (if that's the reason you're trying to access the control). – keyboardP Apr 23 '13 at 20:42
  • 1
    Ah, I passed in the DataTemplate directly and not the DependencyObject from LoadContent(). It works now, thank you! – Markus Persson Apr 24 '13 at 07:03
  • Another quick question: Do I have to reload my LongListSelector template after adding children to my StackPanel? I add some content but it wont show up. Any ideas? – Markus Persson Apr 24 '13 at 07:16
  • Unfortunately I can't test at the moment but this post might help :) http://stackoverflow.com/questions/8846273/adding-children-to-stackpanel-programmatically-not-working – keyboardP Apr 24 '13 at 11:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28811/discussion-between-markus-persson-and-keyboardp) – Markus Persson Apr 24 '13 at 13:10
  • I'm afraid I don't have time but you could post it as a new question and someone else might be able to help. – keyboardP Apr 24 '13 at 13:17

1 Answers1

0

I used the VisualTreeHelper, that keyboardP suggested in the first comment, to find the StackPanel.

You can try using the VisualTreeHelper class to walk the Visual Tree. There's a generic implementation here that may help stackoverflow.com/a/5191681/187697

Markus Persson
  • 1,103
  • 9
  • 21
  • Hi , I am also using LLS , can u please give the solution to access a element inside the LLS Data templete. – Noorul Oct 21 '14 at 12:23