Question

I've got a LongListSelector for which I select the appropriate DataTemplate based on data I receive according to user's selections. There are 3 of those DataTemplates which I define in the page's resources and set the appropriate one -just before populating my LongListSelector- using:

RoutesLongListSelector.ItemTemplate = Resources["SecondItemTemplate"] as DataTemplate;

There is an element in these DataTemplates -a StackPanel- where I add some children after populating my list.

<StackPanel x:Name="MyStations" Grid.Column="1" Grid.Row="1">
</StackPanel>

So, when I try to use its name in order to add children [MyStations.Children.Add(...)] I get this error: 'MyStations' does not exist in the current context. I tried to set one of the Templates as default in the page's ContentPanel but I still get the same error.

Seems to be a minor issue but I couldn't think of something. Any suggestions?

Was it helpful?

Solution

You can't access UI elements of DataTemplete with their Name property, i.e. x:Name. You can use it's loaded event to access it. Please be specific on your requirement.

<StackPanel x:Name="MyStations" Grid.Column="1" Grid.Row="1" 
        Loaded="MyStations_Loaded" />
private void MyStations_Loaded(object sender, RoutedEventArgs e)
{
    var _StackPanel = (StackPanel)sender;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top