wp-VisualTreeExtensions

本文介绍了WPF中视觉树的几种实用操作方法,包括如何检索控件的直接子元素、搜索特定名称的控件、遍历所有逻辑子元素及获取控件的所有父元素等。这些方法有助于开发者更高效地进行界面布局管理和调试。
// (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;
		}
	}
}


[root@yfw im]# head wp-cli.phar #!/usr/bin/env php <?php Phar::mapPhar(); include 'phar://wp-cli.phar/php/boot-phar.php'; __HALT_COMPILER(); ?> wp-cli.phar#vendor/wp-cli/wp-cli/php/wp-cli.php_-h_y)vendor/wp-cli/wp-cli/php/class-wp-cli.php-hO u/vendor/wp-cli/wp-cli/php/fallback-functions.php\-h\'vendor/wp-cli/wp-cli/php/dispatcher.phpX-hXFvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/DeclareFallbackFunctions.phpC-hC@*Bg=vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LoadExecCommand.phpW-hWy~>vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/DeclareMainClass.php-hTjGvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/IncludeFallbackAutoloader.php-h|m:vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php-hA^UBvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LoadUtilityFunctions.php)-h)TYMݤ;vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/BootstrapStep.php-hA7/ Gvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/IncludeRequestsAutoloader.php-h}3X<vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/RunnerInstance.php-hk=vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/ConfigureRunner.php-hB)Q(Hvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/DeclareAbstractBaseCommand.phpA-hA@<@>vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/InitializeLogger.php*-h*\3<vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LoadDispatcher.php5-h5U4Avendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LoadRequiredCommand.php-hӤGvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/RegisterFrameworkCommands.phpg-hg>V<vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/BootstrapState.php-hפ@vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/InitializeContexts.php-h0Ca<vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/AutoloaderStep.php& -h& Dvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/InitializeColorization.phpL-hL'=Hvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/IncludeFrameworkAutoloader.php$-h$z7vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/CheckRoot.php-hFvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/RegisterDeferredCommands.php-hk Fvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/IncludePackageAutoloader.php-hFEvendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/DefineProtectedCommands.php-h8>20vendor/wp-cli/wp-cli/php/WP_CLI/Configurator.php+-h+Αˤ2vendor/wp-cli/wp-cli/php/WP_CLI/SynopsisParser.php-h Evendor/wp-cli/wp-cli/php/WP_CLI/Exception/NonExistentKeyException.php-h4-vendor/wp-cli/wp-cli/php/WP_CLI/Extractor.php>'-h>'m9vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php6-h6ھ=vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php"-h"n?vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CompositeCommand.php -h ڤ>vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandAddition.php#-h#躛:vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/RootCommand.phpK-hKyդ?vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandNamespace.php-h,{*vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php-hY#g1vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/User.php -h ==R 3vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/Signup.phpt-htlYQj1vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/Base.php-h󯹤4vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/Comment.php|-h|m1vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/Site.php-hF)1vendor/wp-cli/wp-cli/php/WP_CLI/Fetchers/Post.php8-h8重.vendor/wp-cli/wp-cli/php/WP_CLI/ComposerIO.php-hмT/vendor/wp-cli/wp-cli/php/WP_CLI/Completions.php-hΤ.vendor/wp-cli/wp-cli/php/WP_CLI/ProcessRun.php(-h(c홤1vendor/wp-cli/wp-cli/php/WP_CLI/Loggers/Quiet.php -h -n0vendor/wp-cli/wp-cli/php/WP_CLI/Loggers/Base.ph-3T3vendor/wp-cli/wp-cli/php/WP_CLI/Loggers/Regular.ph-[Ф5vendor/wp-cli/wp-cli/php/WP_CLI/Loggers/Execution.phpa-hatW3vendor/wp-cli/wp-cli/php/WP_CLI/RequestsLibrary.php--vendor/wp-cli/wp-cli/php/WP_CLI/Inflector.phpE@-hE@#7+vendor/wp-cli/wp-cli/php/WP_CLI/Context.phpM-hM[NG(vendor/wp-cli/wp-cli/php/WP_CLI/NoOp.php-h!-vendor/wp-cli/wp-cli/php/WP_CLI/FileCache.phpC"-hC"0#/2vendor/wp-cli/wp-cli/php/WP_CLI/ContextManager.php-hZϤ7vendor/wp-cli/wp-cli/php/WP_CLI/Iterators/Exception.phpg-hg 43vendor/wp-cli/wp-cli/php/WP_CLI/Iterators/Query.php -h (3vendor/wp-cli/wp-cli/php/WP_CLI/Iterators/Table.phpd -hd [root@yfw im]#
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值