Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
I want to loop through controls nested in a grid in wpf
Abbas Hamza
8y
12.3k
1
Reply
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
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Plot data points on chart with mouse click event dynamically
Get the Value of WPF Listview rows