Search code examples
c#wpfwpf-controls

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.


Solution

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