// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Media;
namespace System.Windows.Controls
{
/// <summary>
/// A static class providing methods for working with the visual tree.
/// </summary>
internal static class VisualTreeExtensions
{
/// <summary>
/// Retrieves all the visual children of a framework element.
/// 搜索子控件(第一层)
/// </summary>
/// <param name="parent">The parent framework element.</param>
/// <returns>The visual children of the framework element.</returns>
internal static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject parent)
{
Debug.Assert(parent != null, "The parent cannot be null.");
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int counter = 0; counter < childCount; counter++)
{
yield return VisualTreeHelper.GetChild(parent, counter);
}
}
/// <summary>
/// 搜索指定名称的控件
/// </summary>
/// <param name="parent"></param>
/// <param name="name"></param>
/// <param name="applyTemplates"></param>
/// <returns></returns>
internal static FrameworkElement GetFirstLogicalChildByName(this FrameworkElement parent, string name, bool applyTemplates)
{
Debug.Assert(parent != null, "The parent cannot be null.");
Queue<FrameworkElement> queue = new Queue<FrameworkElement>();
queue.Enqueue(parent);
while (queue.Count > 0)
{
FrameworkElement element = queue.Dequeue();
var elementAsControl = element as Control;
if (applyTemplates && elementAsControl != null)
{
elementAsControl.ApplyTemplate();
}
if (element.Name.Equals(name) && element != parent)
{
return element;
}
foreach (FrameworkElement visualChild in element.GetVisualChildren().OfType<FrameworkElement>())
{
queue.Enqueue(visualChild);
}
}
return null;
}
/// <summary>
/// Retrieves all the logical children of a framework element using a
/// breadth-first search. A visual element is assumed to be a logical
/// child of another visual element if they are in the same namescope.
/// For performance reasons this method manually manages the queue
/// instead of using recursion.
/// 搜索子控件(所有)
/// </summary>
/// <param name="parent">The parent framework element.</param>
/// <returns>The logical children of the framework element.</returns>
internal static IEnumerable<FrameworkElement> GetLogicalChildrenBreadthFirst(this FrameworkElement parent)
{
Debug.Assert(parent != null, "The parent cannot be null.");
Queue<FrameworkElement> queue =
new Queue<FrameworkElement>(parent.GetVisualChildren().OfType<FrameworkElement>());
while (queue.Count > 0)
{
FrameworkElement element = queue.Dequeue();
yield return element;
foreach (FrameworkElement visualChild in element.GetVisualChildren().OfType<FrameworkElement>())
{
queue.Enqueue(visualChild);
}
}
}
/// <summary>
/// Gets the ancestors of the element, up to the root, limiting the
/// ancestors by FrameworkElement.
/// 获取所有父控件
/// </summary>
/// <param name="node">The element to start from.</param>
/// <returns>An enumerator of the ancestors.</returns>
internal static IEnumerable<FrameworkElement> GetVisualAncestors(this FrameworkElement node)
{
FrameworkElement parent = node.GetVisualParent();
while (parent != null)
{
yield return parent;
parent = parent.GetVisualParent();
}
}
/// <summary>
/// Gets the visual parent of the element.
/// 获取父控件
/// </summary>
/// <param name="node">The element to check.</param>
/// <returns>The visual parent.</returns>
internal static FrameworkElement GetVisualParent(this FrameworkElement node)
{
return VisualTreeHelper.GetParent(node) as FrameworkElement;
}
/// <summary>
/// The first parent of the framework element of the specified type
/// that is found while traversing the visual tree upwards.
/// 根据类型获取父控件
/// </summary>
/// <typeparam name="T">
/// The element type of the dependency object.
/// </typeparam>
/// <param name="element">The dependency object element.</param>
/// <returns>
/// The first parent of the framework element of the specified type.
/// </returns>
internal static T GetParentByType<T>(this DependencyObject element)
where T : FrameworkElement
{
Debug.Assert(element != null, "The element cannot be null.");
T result = null;
DependencyObject parent = VisualTreeHelper.GetParent(element);
while (parent != null)
{
result = parent as T;
if (result != null)
{
return result;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
}
}
// (c) Copyright Microsoft Corporation.// This source is subject to the Microsoft Public License (Ms-PL).// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.// All other rights