zoukankan      html  css  js  c++  java
  • wpf 查找父元素、子元素方法

    1         /// <summary>
     2         /// 根据类型查找子元素
     3         /// </summary>
     4         /// <typeparam name="T"></typeparam>
     5         /// <param name="obj"></param>
     6         /// <param name="typename"></param>
     7         /// <returns></returns>
     8         public List<T> GetChildObjects<T>(DependencyObject obj, Type typename) where T : FrameworkElement
     9         {
    10             DependencyObject child = null;
    11             List<T> childList = new List<T>();
    12 
    13             for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
    14             {
    15                 child = VisualTreeHelper.GetChild(obj, i);
    16 
    17                 if (child is T && (((T)child).GetType() == typename))
    18                 {
    19                     childList.Add((T)child);
    20                 }
    21                 childList.AddRange(GetChildObjects<T>(child, typename));
    22             }
    23             return childList;
    24         }
    复制代码

    调用 1 List<StackPanel> initToolBarWeChatUserSp = GetChildObjects<StackPanel>(name,typeof(StackPanel)); 

    复制代码
     1         /// <summary>
     2         /// 查找父元素
     3         /// </summary>
     4         /// <typeparam name="T"></typeparam>
     5         /// <param name="obj"></param>
     6         /// <param name="name"></param>
     7         /// <returns></returns>
     8         public static T FindParent<T>(DependencyObject i_dp) where T : DependencyObject
     9         {
    10             DependencyObject dobj = (DependencyObject)VisualTreeHelper.GetParent(i_dp);
    11             if (dobj != null)
    12             {
    13                 if (dobj is T)
    14                 {
    15                     return (T)dobj;
    16                 }
    17                 else
    18                 {
    19                     dobj = FindParent<T>(dobj);
    20                     if (dobj != null && dobj is T)
    21                     {
    22                         return (T)dobj;
    23                     }
    24                 }
    25             }
    26             return null;
    27         }
    复制代码

    调用 1 Border tbBorder = FindParent<Border>(name); 

  • 相关阅读:
    Git的基本使用(只是基本使用)
    GET与POST的比较
    GO开发:链表
    阿里云啊
    以太坊区块和交易存储
    以太坊私有链部署合约
    以太坊go-ethereum签名部分源码解析
    GO开发:接口
    区块链开发:以太坊网络
    Go开发[八]goroutine和channel
  • 原文地址:https://www.cnblogs.com/sjqq/p/6838351.html
Copyright © 2011-2022 走看看