// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections.Generic; using System.Linq; using Perspex.Controls; using Perspex.Styling; public static class ControlExtensions { // TODO: This needs to traverse the logical tree, not the visual. public static T FindControl(this Control control, string id) where T : Control { return control.GetVisualDescendents() .OfType() .FirstOrDefault(x => x.Id == id); } public static IEnumerable GetTemplateControls(this ITemplatedControl control) { return GetTemplateControls(control, (IVisual)control); } public static IEnumerable GetTemplateControls(ITemplatedControl templated, IVisual parent) { IVisual visual = parent as IVisual; foreach (var child in visual.VisualChildren.OfType().Where(x => x.TemplatedParent != null)) { yield return child; foreach (IVisual grandchild in GetTemplateControls(templated, child)) { yield return (Control)grandchild; } } } } }