wp-VisualTreeExtensions

// (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;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值