Skip to content

Instantly share code, notes, and snippets.

@judismith
Created December 31, 2020 19:23
Show Gist options
  • Save judismith/5b11c3d864bf446e770a70366d390e5a to your computer and use it in GitHub Desktop.
Save judismith/5b11c3d864bf446e770a70366d390e5a to your computer and use it in GitHub Desktop.
Xamarin : Get all the child controls of type<T> of a form by sending in the root control (ContentPage)
private static IList<T> GetAllControlsOfType<T>(Element view) where T : View
{
var rtn = new List<T>();
foreach (View item in view.Descendants())
{
var ctr = item as T;
if (ctr != null)
{
rtn.Add(ctr);
}
else
{
rtn.AddRange(GetAllControlsOfType<T>(item));
}
}
return rtn;
}
//Hat Tip: https://stackoverflow.com/a/23214747/8205973
// Example Usage: IList<SfTextInputLayout> inputs = GetAllControlsOfType<SfTextInputLayout>(SignUpForm);
// where "SignUpForm" is the x:Name property of the ContentPage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment