TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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 :
<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
{
// Confirm parent and childName are valid.
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);
// If the child is not of the request child type child
T childType = child
as
T;
if
(childType ==
null
)
{
// recursively drill down the tree
foundChild = FindChild<T>(child, childName);
// If the child is found, break so we do not overwrite the found child.
if
(foundChild !=
null
)
break
;
}
else
if
(!
string
.IsNullOrEmpty(childName))
{
var frameworkElement = child
as
FrameworkElement;
// If the child's name is set for search
if
(frameworkElement !=
null
&& frameworkElement.Name == childName)
{
// if the child's name is of the request name
foundChild = (T)child;
break
;
}
}
else
{
// child element found.
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
Reply
Answers (
1
)
Plot data points on chart with mouse click event dynamically
Get the Value of WPF Listview rows