简体   繁体   中英

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 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.

在WPF中,您应该尝试this.FindName(string name)

Button b = (Button)this.FindName("button1");

您提供的链接是针对Winforms的,您正在寻找一种WPF方式来实现此目的,这是不同的。

If you are looking to iterate through the controls for whatever reason, in your Window class, you can iterate through the LayoutRoot's children (eg)

    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.

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;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM