I have this grid with button and labels :
- <Grid>
- <Grid Height="95" VerticalAlignment="Top"
- Background="{StaticResource TopBar}">
- <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"/>
- <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>
- <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">
- <Button.Background>
- <ImageBrush ImageSource="/Images/LibraryCatalog.png"/>
- </Button.Background>
- </Button>
- </Grid>
- </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
- public static T FindChild<T>(DependencyObject parent, string childName)
- where T : DependencyObject
- {
-
- if (parent == null) return null;
-
- T foundChild = null;
-
- int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = 0; i < childrenCount; i++)
- {
- var child = VisualTreeHelper.GetChild(parent, i);
-
- T childType = child as T;
- if (childType == null)
- {
-
- foundChild = FindChild<T>(child, childName);
-
-
- if (foundChild != null) break;
- }
- else if (!string.IsNullOrEmpty(childName))
- {
- var frameworkElement = child as FrameworkElement;
-
- if (frameworkElement != null && frameworkElement.Name == childName)
- {
-
- foundChild = (T)child;
- break;
- }
- }
- else
- {
-
- foundChild = (T)child;
- break;
- }
- }
-
- return foundChild;
- }
then use it in code behind as follow:
- private void Set_Language()
- {
- Label lblText = UIHelper.FindChild<Label>(Application.Current.MainWindow, "lblWebLinks");
-
-
- strLanguage = "LibraryAccess.Languages." + ((ComboBoxItem)ddlLanguage.SelectedItem).Name.ToString();
- ResourceManager LocRM = new ResourceManager(strLanguage, typeof(MainMenu).Assembly);
-
-
- if (lblText != null)
- {
- lblText.Content = LocRM.GetString("lblWebLinks");
- }
-
-
- }
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