// (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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace System.Windows.Controls
{
///
/// A static class providing methods for working with the visual tree.
///
internal static class VisualTreeExtensions
{
///
/// Retrieves all the visual children of a framework element.
///
/// The parent framework element.
/// The visual children of the framework element.
internal static IEnumerable 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);
}
}
///
/// 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.
///
/// The parent framework element.
/// The logical children of the framework element.
internal static IEnumerable GetLogicalChildrenBreadthFirst(this FrameworkElement parent)
{
Debug.Assert(parent != null, "The parent cannot be null.");
Queue queue =
new Queue(parent.GetVisualChildren().OfType());
while (queue.Count > 0)
{
FrameworkElement element = queue.Dequeue();
yield return element;
foreach (FrameworkElement visualChild in element.GetVisualChildren().OfType())
{
queue.Enqueue(visualChild);
}
}
}
}
}