diff --git a/src/Perspex.Controls/ControlExtensions.cs b/src/Perspex.Controls/ControlExtensions.cs index 0c3831289f..81f3eed8f1 100644 --- a/src/Perspex.Controls/ControlExtensions.cs +++ b/src/Perspex.Controls/ControlExtensions.cs @@ -1,6 +1,7 @@ // Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; using System.Linq; using Perspex.LogicalTree; using Perspex.Styling; @@ -42,14 +43,22 @@ namespace Perspex.Controls /// Finds the named control in the specified control. /// /// The type of the control to find. - /// The control. + /// The control to look in. /// The name of the control to find. /// The control or null if not found. - public static T FindControl(this IControl control, string name) where T : IControl + public static T FindControl(this IControl control, string name) where T : class, IControl { - return control.GetLogicalDescendents() - .OfType() - .FirstOrDefault(x => x.Name == name); + var nameScope = control.GetSelfAndLogicalAncestors() + .OfType() + .Select(x => (x as INameScope) ?? NameScope.GetNameScope(x)) + .FirstOrDefault(x => x != null); + + if (nameScope == null) + { + throw new InvalidOperationException("Could not find parent name scope."); + } + + return nameScope.Find(name); } } }