How do I go through all the controls in a window in WPF?

3

Is there any way I can go through all the controls in a window and disable them? For example, make a foreach that disables one by one of the controls. Something like below, only for WPF, not WinForms.

private void HabilitarControles(bool habilita)
    {
        foreach (Control c in Controls)
        {
            c.Enabled = habilita;
            foreach (Control con in c.Controls.OfType<Control>())
                con.Enabled = habilita;
        }
    }
    
asked by anonymous 31.08.2015 / 14:40

1 answer

3

You do not have to do this one by one. You probably put them inside a Panel (if not, should), disable only Panel . All will be disabled.

If you still insist on doing what you already know how to do:

private void HabilitarControles(bool habilita) {
    foreach (Control c in this.Controls) {
        c.Enabled = habilita;
    }
}

If you want some other sophistication, just adapt it. There are no other requirements in the question.

    
31.08.2015 / 14:48