0

I have a panorama control in which I have create a header template to add a list picker inside it. (Just like the peoples hub to select social accounts)

<DataTemplate x:Key="PanoramaItemHeaderTemplate">
     <ContentPresenter>
          <StackPanel Height="129">
               <TextBlock Text="{Binding}" FontSize="72" Margin="0,7,0,0" />
               <toolkit:ListPicker x:Name="listPick" Margin="0,-21,0,0" BorderThickness="0">
                     <toolkit:ListPickerItem Content="twitter"></toolkit:ListPickerItem>
               </toolkit:ListPicker>                    
          </StackPanel>
     </ContentPresenter>
</DataTemplate>

The panorama control is inside the MainPage.xaml file and I want to have access to the listpicker from the code behind to be able to populate it, and handle it selection events.

I don't know how to do this. I tried adding the x:name property to the list picker I don't have access to it in the MainPage code behind.

Any idea on how to approach this is very welcomed, thanks!

blackjid
  • 1,571
  • 16
  • 23

3 Answers3

1

From what you have now, the quickest way to do what you want is to traverse the visual tree

See here for the implementation:

How to access a specific item in a Listbox with DataTemplate?

Community
  • 1
  • 1
Igor Ralic
  • 14,975
  • 4
  • 43
  • 51
  • I tried the helper function that solve the question you mention. But nothing happen I get null. I tried adding a x:name to the panoramaitem and then calling ListPicker listPicker1 = FindDecendant(myPanoramaItem) but returns null. Any idea? – blackjid Jul 15 '12 at 14:08
  • this is weird. You are either trying to do the search before the items are added and the visual tree created, or the listpicker isn't there. Have you tried searching the whole page object (not just the panorama item)? Can you try adding the following line as the first line of the FindDescendant method: Debug.WriteLine(obj.ToString()); so you can see what exactly is in the tree and to see whether your panoramaitem is added correctly? – Igor Ralic Jul 15 '12 at 14:29
  • How do I know if the visualtree is created already? I'm trying this in the MainPage constructor after calling the InitializeComponent() method. I have added the WriteLine(obj.ToString()) but it only print the panoramaItem, nothing more – blackjid Jul 17 '12 at 19:12
  • Thanks, you where right. The PanoramaItems where not created yet. I used a handler for the loaded event of the PanoramaControl, and there I used the FindDescendant method and worked fine! – blackjid Jul 18 '12 at 03:28
0

You cannot access the ListPicker by x:Name because it is not unambiguous: there is a ListPicker generated for each PanoramaItem in your Panorama. So the first question is, is it really the think you want to do? If so you need to populate it using a binding (ItemSource)

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
  • Yes, I understand that. I want to have a different listpicker inside the header of **each** panoramaitem. I want to populate it in the code behind because of the listpicker bug that prevents me to use the xaml listpickeritem if I want to use the fullscreenmode. But I still doesn't understand how to access each instance of the listpicker from the code behind to populate it and to add the event handlers... – blackjid Jul 14 '12 at 17:03
0

You can access an element inside another resource, consider this example:

<Grid Name="myGrid">
<StackPanel x:Name="stack1">
<TextBlock x:Name="abc"/>
</StackPanel>
</Grid>

We can only access the Grid myGrid in code by default. To get reference to the StackPanel we can do this:

StackPanel myStack=myGrid.FindName("stack1") as Stackpanel;

After that we can get reference to the TextBlock:

TextBlock myTextBlock=myStack.FindName("abc") as TextBlock;

You can modify myTextBlock after that as you may like. You can apply the same technique in your case and it will work. Hope that helps :).

naqvitalha
  • 793
  • 5
  • 9
  • I tried adding a name to every element in the template and the panoramaitem but nothing happend I get null. I only have access to the panoramaitem, from there I tried to findName for every element, but always returned null. Any idea? – blackjid Jul 15 '12 at 14:10