//------------------------------------------------------ // // Constructors // //------------------------------------------------------ //------------------------------------------------------ // // Public Properties // //------------------------------------------------------ // None //------------------------------------------------------ // // Public Methods // //------------------------------------------------------ #region Public Methods /// <summary> /// Given an element in the logical tree to start searching from, /// searches all its descendent nodes in the logical tree a node whose Name /// matches the specified elementName. /// The given DependencyObject must be either a FrameworkElement or FrameworkContentElement. /// </summary> /// <remarks> /// We're searching in a depth-first manner. Review this if this turns out /// to be a performance problem. We're doing this first because it's easy /// and light on memory usage as compared to breadth-first. /// (RogerCh):It would be cool if the DFID (depth-first iterative-deepening) /// algorithm would be useful here. /// </remarks> public static DependencyObject FindLogicalNode(DependencyObject logicalTreeNode, string elementName) { if (logicalTreeNode == null) { throw new ArgumentNullException("logicalTreeNode"); } if (elementName == null) { throw new ArgumentNullException("elementName"); } if (elementName == String.Empty) { throw new ArgumentException(SR.Get(SRID.StringEmpty), "elementName"); } DependencyObject namedElement = null; DependencyObject childNode = null; // Check given node against named element. IFrameworkInputElement selfNode = logicalTreeNode as IFrameworkInputElement; if (selfNode != null) { if (selfNode.Name == elementName) { namedElement = logicalTreeNode; } } if (namedElement == null) { // Nope, the given node isn't it. See if we can check children. IEnumerator childEnumerator = null; childEnumerator = LogicalTreeHelper.GetLogicalChildren(logicalTreeNode); // If we can enumerate, check the children. if (childEnumerator != null) { childEnumerator.Reset(); while (namedElement == null && childEnumerator.MoveNext() == true) { childNode = childEnumerator.Current as DependencyObject; if (childNode != null) { namedElement = FindLogicalNode(childNode, elementName); } } } } // Return what we can find - may be null. return(namedElement); }
/// <summary>Attempts to find and return an object that has the specified name. The search starts from the specified object and continues into subnodes of the logical tree. </summary> /// <param name="logicalTreeNode">The object to start searching from. This object must be either a <see cref="T:System.Windows.FrameworkElement" /> or a <see cref="T:System.Windows.FrameworkContentElement" />.</param> /// <param name="elementName">The name of the object to find.</param> /// <returns>The object with the matching name, if one is found; returns <see langword="null" /> if no matching name was found in the logical tree.</returns> // Token: 0x06000763 RID: 1891 RVA: 0x00017104 File Offset: 0x00015304 public static DependencyObject FindLogicalNode(DependencyObject logicalTreeNode, string elementName) { if (logicalTreeNode == null) { throw new ArgumentNullException("logicalTreeNode"); } if (elementName == null) { throw new ArgumentNullException("elementName"); } if (elementName == string.Empty) { throw new ArgumentException(SR.Get("StringEmpty"), "elementName"); } DependencyObject dependencyObject = null; IFrameworkInputElement frameworkInputElement = logicalTreeNode as IFrameworkInputElement; if (frameworkInputElement != null && frameworkInputElement.Name == elementName) { dependencyObject = logicalTreeNode; } if (dependencyObject == null) { IEnumerator logicalChildren = LogicalTreeHelper.GetLogicalChildren(logicalTreeNode); if (logicalChildren != null) { logicalChildren.Reset(); while (dependencyObject == null && logicalChildren.MoveNext()) { DependencyObject dependencyObject2 = logicalChildren.Current as DependencyObject; if (dependencyObject2 != null) { dependencyObject = LogicalTreeHelper.FindLogicalNode(dependencyObject2, elementName); } } } } return(dependencyObject); }