Reputation: 9255
Sorry for a potentially dumb question, I am still new at this. I really appreciate your help. Referring to Get a Windows Forms control by name in C# But I don't have a "this.Controls" available. Is there something I am missing here?
In other words, when I type "this." and visual studio populates a list of option, there is no "Controls" option.
Upvotes: 1
Views: 2358
Reputation: 71
Yes, this is very important issue when we want to make a change in similar controls in a particular Grid. So after looking up for a while I found out one solution which looks better.
I am performing a Null or Empty check on all the textboxes in my Grid when someone randomly presses the SUBMIT Button
Here is the C# Codeblock
foreach (TextBox tx in Grid1.Children.OfType<TextBox>())
{
if (string.IsNullOrEmpty(tx.Text))
{
MessageBox.Show("No empty boxes please!");
return;
}
}
Upvotes: 0
Reputation: 1
I have to add whats LayoutRoot: because I am new in WPF I did not know whats that
After general research I found them: it's your Grid if you write grid visual studio did not find them what you should do?
go to your window
next go to the Grid Start tag : <grid>
next add : x:Name="your desired name"
like this : <Grid x:Name="FRM">
now go back to code yourwindow.xaml.cs
not you see it works foreach (object o in this.FRM.Children)
I hope it was useful for a novice like me
Upvotes: 0
Reputation: 23157
If you are looking to iterate through the controls for whatever reason, in your Window class, you can iterate through the LayoutRoot's children (e.g.)
foreach (object o in this.LayoutRoot.Children)
{
MessageBox.Show(o.GetType().Name);
}
Keep in mind that the children can also contain children, so you'll need to delve into each of those as needed.
Upvotes: 1
Reputation: 126992
In WPF, you should try this.FindName(string name)
Button b = (Button)this.FindName("button1");
Upvotes: 4
Reputation: 347606
The link you gave was for Winforms, you are looking for a WPF way to do it which is different.
Upvotes: 1