Fedor
Fedor

Reputation: 43

How to find all the controls in WPF on a UserControl component

I can get all UI controls on a Form but how to find controls on a certain UserControl?

Upvotes: 4

Views: 10416

Answers (3)

Bill Tarbell
Bill Tarbell

Reputation: 5234

If you only want to look in the immediate object you can do FindName:

object foundControl = someParentControl.FindName("nameOfChild");

or if you want recursive ways then you can look at this post here: How can I find WPF controls by name or type?

Upvotes: 1

TorbenJ
TorbenJ

Reputation: 4582

You may have a look at the FindName(string) method of UserControl

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can use Linq operator OfType and Controls property

var controls = YourForm
               .YourUserControl
               .Controls.OfType<TextBox>();
foreach(var control in controls)
{
    ....
}

Link : http://msdn.microsoft.com/fr-fr/library/vstudio/bb360913.aspx

Upvotes: 5

Related Questions