WPF中找指定类型控件列表 (UIElementExtensions.cs)

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Telerik.Windows.Controls
{
 /// <summary>
 /// Contains helper extension methods for the UIElement class.
 /// </summary>
 public static class UIElementExtensions
 {
  /// <summary>
  /// Gets the parent element from the visual tree by given type.
  /// </summary>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
  public static T ParentOfType<T>(this UIElement element) where T : DependencyObject
  {
   if (element == null) return null;

   DependencyObject parent = VisualTreeHelper.GetParent(element);

   while ((parent != null) && !(parent is T))
   {
    DependencyObject newVisualParent = VisualTreeHelper.GetParent(parent);
    if (newVisualParent != null)
    {
     parent = newVisualParent;
    }
    else
    {
     // try to get the logical parent ( e.g. if in Popup)
     if (parent is FrameworkElement)
     {
      parent = (parent as FrameworkElement).Parent;
     }
     else
     {
      break;
     }
    }
   }

   return parent as T;
  }

  /// <summary>
  /// Gets all child elements recursively from the visual tree by given type.
  /// </summary>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
  public static IList<T> ChildrenOfType<T>(this UIElement element) where T : UIElement
  {
   List<T> list = new List<T>();

   if (element != null)
   {
    element.ChildrenOfType<T>(ref list);
   }

   return list;
  }

  private static IList<T> ChildrenOfType<T>(this UIElement element, ref List<T> list) where T : UIElement
  {
   int count = VisualTreeHelper.GetChildrenCount(element);

   for (int i = 0; i < count; i++)
   {
    DependencyObject child = VisualTreeHelper.GetChild(element, i);

    if (child is T)
    {
     list.Add((T)child);
    }

    var uiElementChild = child as UIElement;
    if (uiElementChild != null)
    {
     uiElementChild.ChildrenOfType<T>(ref list);
    }
   }

   return list;
  }

  internal static bool IsAncestorOf(this UIElement target, DependencyObject element)
  {
   if (target == element)
   {
    return true;
   }

   if (element == null)
   {
    throw new System.ArgumentNullException("element");
   }

   if (!(element is UIElement))
   {
    throw new InvalidOperationException("element is not UIElement");
   }

   DependencyObject parent = element;
   do
   {
    DependencyObject currentParent = VisualTreeHelper.GetParent(parent);
    var feParent = parent as FrameworkElement;
    if (currentParent == null && feParent != null)
    {
     parent = feParent.Parent;
    }
    else
    {
     parent = currentParent;
    }

    if (parent == target)
    {
     return true;
    }
   }
   while (parent != null);
   return false;
  }

#if SILVERLIGHT
  internal static void GoToState(this Control element, bool useTransitions, params string[] stateNames)
  {
   if (stateNames != null)
   {
    foreach (string str in stateNames)
    {
     if (VisualStateManager.GoToState(element, str, useTransitions))
     {
      return;
     }
    }
   }
  }
#endif
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值