Can't find this.Controls - what am I missing?
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 somethi开发者_如何学运维ng I am missing here?
In other words, when I type "this." and visual studio populates a list of option, there is no "Controls" option.
In WPF, you should try this.FindName(string name)
Button b = (Button)this.FindName("button1");
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.
The link you gave was for Winforms, you are looking for a WPF way to do it which is different.
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
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;
}
}
精彩评论