How to find/access the Control inside the LongListSelector of Windows Phone 8/7 App

To find the Control Whether it may be a Checkbox,TextBox,Loader,ImageBrush whatever it may be By using the Following code you can find the control in LongListSelector and do respective actions like enabling,disabling,hiding etc.For Example this is my LongListSelector I need to find a control and Hide it

  1. <phone:LongListSelector  
  2.  x:Name="AddrBook"  
  3.  JumpListStyle="{StaticResource AddrBookJumpListStyle}"  
  4.  Background="Transparent"  
  5.  GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"  
  6.  ItemTemplate="{StaticResource AddrBookItemTemplate}"  
  7.  LayoutMode="List"  
  8.  IsGroupingEnabled="true"  
  9. HideEmptyGroups ="true"/>  

This is my Data Template of LongListSelector.

  1. <DataTemplate x:Key="AddrBookItemTemplate">  
  2.    <StackPanel VerticalAlignment="Top">  
  3.        <TextBlock FontWeight="Bold"  Text="{Binding FirstName}" />  
  4.        <TextBlock Text="{Binding LastName}" />  
  5.        <TextBlock Text="{Binding Address}" />  
  6.        <TextBlock Text="{Binding Phone}" />  
  7.    </StackPanel>  
  8. </DataTemplate>  

So What can I Do is I can Simply use the following code to get what control I want from the longlistselector .

Use the Below Code Wherever you wish to get Control inside LLS

  1. LongListSelector myControl= (AddrBook as LongListSelector); 
  2. SearchVisualTree(myControl); //Finding The TextBlock In LongListSelector(in My Case)
  1. private void SearchVisualTree(DependencyObject targetElement)  
  2. {  
  3.     var count = VisualTreeHelper.GetChildrenCount(targetElement);  
  4.     if (count == 0)  
  5.         return;  
  6.     for (int i = 0; i < count; i++)  
  7.     {  
  8.         var child = VisualTreeHelper.GetChild(targetElement, i);  
  9.         if (child is TextBlock) //Setting Control to be Find                  {  
  10.             TextBlock targetItem = (TextBlock)child;  
  11.   
  12.   
  13.             if (targetItem.Visibility == System.Windows.Visibility.Visible)  
  14.             {  
  15.                 targetItem.Visibility = System.Windows.Visibility.Collapsed;  
  16.                 return//Setting visual Property of TextBlock to Collapsed  
  17.             }  
  18.         }  
  19.         else  
  20.         {  
  21.             SearchVisualTree(child);  
  22.         }  
  23.     }  
  24. }  
Now its Done!Cheers

Thank You !

Ebook Download
View all
Learn
View all