Abbas Hamza

Abbas Hamza

  • NA
  • 100
  • 52.8k

I want to loop through controls nested in a grid in wpf

Apr 21 2016 5:09 AM
I have this grid with button and labels :
  1. <Grid>  
  2.            <Grid Height="95" VerticalAlignment="Top"  
  3.         Background="{StaticResource TopBar}">  
  4.                <Button Style="{StaticResource MyButtonStyle}" Content="" HorizontalAlignment="Left"  Margin="1208,8.04,0,0" VerticalAlignment="Top" Width="185" Height="77" ToolTip="Main Catalogue " Cursor="Hand" Click="Button_Click"/>  
  5.                <TextBlock Height="40" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="200" TextWrapping="Wrap" FontSize="21.333"><Run Language="en-gb" Text="Promotions"/></TextBlock>  
  6.                <Button Style="{StaticResource MyButtonStyle}" Content="" HorizontalAlignment="Left" Margin="304,8,0,0" VerticalAlignment="Top" Width="185" Height="77" ToolTip="Main Catalogue " Cursor="Hand" Click="Button_Click">  
  7.                    <Button.Background>  
  8.                        <ImageBrush ImageSource="/Images/LibraryCatalog.png"/>  
  9.                    </Button.Background>  
  10.                </Button>  
  11.            </Grid>  
  12.        </Grid>  
in code behind i managed to use the following method found online to find one instance of a button then locate the label for that button for the purpose of changing the language in a resource file accordingly
  1. public static T FindChild<T>(DependencyObject parent, string childName)  
  2.           where T : DependencyObject  
  3.        {  
  4.            // Confirm parent and childName are valid.   
  5.            if (parent == nullreturn null;  
  6.   
  7.            T foundChild = null;  
  8.   
  9.            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);  
  10.            for (int i = 0; i < childrenCount; i++)  
  11.            {  
  12.                var child = VisualTreeHelper.GetChild(parent, i);  
  13.                // If the child is not of the request child type child  
  14.                T childType = child as T;  
  15.                if (childType == null)  
  16.                {  
  17.                    // recursively drill down the tree  
  18.                    foundChild = FindChild<T>(child, childName);  
  19.   
  20.                    // If the child is found, break so we do not overwrite the found child.   
  21.                    if (foundChild != nullbreak;  
  22.                }  
  23.                else if (!string.IsNullOrEmpty(childName))  
  24.                {  
  25.                    var frameworkElement = child as FrameworkElement;  
  26.                    // If the child's name is set for search  
  27.                    if (frameworkElement != null && frameworkElement.Name == childName)  
  28.                    {  
  29.                        // if the child's name is of the request name  
  30.                        foundChild = (T)child;  
  31.                        break;  
  32.                    }  
  33.                }  
  34.                else  
  35.                {  
  36.                    // child element found.  
  37.                    foundChild = (T)child;  
  38.                    break;  
  39.                }  
  40.            }  
  41.   
  42.            return foundChild;  
  43.        }  
then use it in code behind as follow:
  1. private void Set_Language()  
  2.       {  
  3.           Label lblText = UIHelper.FindChild<Label>(Application.Current.MainWindow, "lblWebLinks");  
  4.            
  5.            
  6.           strLanguage = "LibraryAccess.Languages." + ((ComboBoxItem)ddlLanguage.SelectedItem).Name.ToString();  
  7.           ResourceManager LocRM = new ResourceManager(strLanguage, typeof(MainMenu).Assembly);  
  8.   
  9.   
  10.           if (lblText != null)  
  11.           {  
  12.               lblText.Content = LocRM.GetString("lblWebLinks");  
  13.           }  
  14.   
  15.   
  16.       }  
My question is the method above it could only locate one label by name, is there any way i could loop trough each one of the label on the above grid for each button then use the set_language method to loop then use something like: foreach label in grid x.
please advise 

Answers (1)