private DependencyObject FindControl(string id, DependencyObject currentParent) { var currChildren = currentParent.GetVisualChildren(); foreach (var item in currChildren) { if (item.GetValue(NameProperty).ToString().Equals(id)) return item; var childItem = FindControl(id, item); if (childItem != null) return childItem; } return null; }
/// <summary> /// Finds a logical node in the tree of the specified <see cref="DependencyObject"/>. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="name">The name of the control to find.</param> /// <returns>Child as <see cref="DependencyObject"/> or <c>null</c> if the child cannot be found.</returns> /// <exception cref="ArgumentNullException">The <paramref name="dependencyObject"/> is <c>null</c>.</exception> /// <exception cref="ArgumentException">The <paramref name="name"/> is <c>null</c> or whitespace.</exception> public static DependencyObject FindLogicalNode(DependencyObject dependencyObject, string name) { Argument.IsNotNull("dependencyObject", dependencyObject); Argument.IsNotNullOrWhitespace("name", name); // Check if the object is the node itself if (IsElementWithName(dependencyObject, name)) { return dependencyObject; } // Search all child nodes var children = new List<DependencyObject>(dependencyObject.GetVisualChildren()); foreach (DependencyObject child in children) { if (IsElementWithName(child, name)) { return child; } } // Since we didn't find anything, check the childs of all childs return children.Select(child => FindLogicalNode(child, name)).FirstOrDefault(foundChild => foundChild != null); }
private bool HasStringInSomePropertyOfItselfOrChildren(DependencyObject node, string value) { foreach (var property in node.GetType().GetProperties()) { if (property.CanRead && property.GetValue(node, null) as string == value) { return true; } } foreach (var child in node.GetVisualChildren()) { if (HasStringInSomePropertyOfItselfOrChildren(child, value)) { return true; } } return false; }
private string GetStringsFromPropertiesOfItselfAndChildren(DependencyObject node, string currentStrings) { foreach (var property in node.GetType().GetProperties()) { if (property.CanRead) { currentStrings += "|" + property.GetValue(node, null); } } foreach (var child in node.GetVisualChildren()) { currentStrings = GetStringsFromPropertiesOfItselfAndChildren(child, currentStrings); } return currentStrings; }
private void FindChildrenByType(DependencyObject parent, Type type, ICollection<DependencyObject> children) { if (parent.GetType() == type) { children.Add(parent); } foreach (var child in parent.GetVisualChildren()) { FindChildrenByType(child, type, children); } }
private string Describe(DependencyObject node, string identation) { var description = identation == string.Empty ? "" : identation.Substring(0, identation.Length - 1) + "-"; description += node; description += !string.IsNullOrEmpty(node.GetValue(NameProperty).ToString()) ? " - " + node.GetValue(NameProperty) : ""; if (node.GetType() == typeof(TextBlock)) { description += " - " + ((TextBlock)node).Text; } description += "\n"; foreach (var child in node.GetVisualChildren()) { description += Describe(child, identation + "| "); } return description; }
protected override IEnumerable<DependencyObject> GetChildren(DependencyObject dependencyObject) { return dependencyObject.GetVisualChildren (); }