查找逻辑子节点,包括隐藏节点和折叠节点 [英] Find logical child including hidden and collapsed nodes

查看:63
本文介绍了查找逻辑子节点,包括隐藏节点和折叠节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到这个问题的答案,在每一篇文章中,我都找到了递归寻找孩子的答案,但他们都没有与隐藏或折叠的孩子一起工作

I have tried finding an answer to this problem and in every post I find there is an answer to finding children recursively but none of them work with hidden or collapsed children

在每个帖子中也有人问这是否可行,但没有人回答,所以我开始认为这是不可能的

Also in every post someone has asked if this is possible but no one has answered so I am beginning to think this isn't possible

如果有人有办法做到这一点,我将永远感激不尽.

If anyone has a way of doing this I will be eternally grateful.

我的函数如下所示:

        public static DependencyObject FindLogicalDescendentByName(this DependencyObject source, string name)
    {
        DependencyObject result = null;
        IEnumerable children = LogicalTreeHelper.GetChildren(source);

        foreach (var child in children)
        {
            if (child is DependencyObject)
            {
                if (child is FrameworkElement)
                {
                    if ((child as FrameworkElement).Name.Equals(name))
                        result = (DependencyObject)child;
                    else
                        result = (child as DependencyObject).FindLogicalDescendentByName(name);
                }
                else
                {
                    result = (child as DependencyObject).FindLogicalDescendentByName(name);
                }
                if (result != null)
                    return result;
            }
        }
        return result;
    }

-编辑所以,我意识到我的问题是我试图在创建之前找到该项目,

-EDITED So, I realise my problem is that I was trying to find the item before it was created,

我正在绑定到 xaml 中的一个属性,该属性会关闭并按给定名称查找项目,但该项目当时并未创建,如果我在 xaml 中重新订购该项目,则它可以工作并且该项目被发现了……哦!

I was binding to a property in xaml that would go off and find an item by a given name, but the item wasnt created at that point in time, if I re-ordered the item in xaml, it works and the item is found... doh!

推荐答案

这是我的代码,如果您指定 X:Name 和类型,它就可以工作

Here is my code, it works if you specify the X:Name and the type

调用示例:

System.Windows.Controls.Image myImage = FindChild<System.Windows.Controls.Image>(this (or parent), "ImageName");



/// <summary>
/// Find specific child (name and type) from parent
/// </summary>
public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
{
   // Confirm parent and childName are valid. 
   if (parent == null) return null;

   T foundChild = null;

   int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
   for (int i = 0; i < childrenCount; i++)
   {
      var child = VisualTreeHelper.GetChild(parent, i);
      // If the child is not of the request child type child
      T childType = child as T;
      if (childType == null)
      {
         // recursively drill down the tree
         foundChild = FindChild<T>(child, childName);

         // If the child is found, break so we do not overwrite the found child. 
         if (foundChild != null) break;
      }
      else if (!string.IsNullOrEmpty(childName))
      {
         var frameworkElement = child as FrameworkElement;
         // If the child's name is set for search
         if (frameworkElement != null && frameworkElement.Name == childName)
         {
            // if the child's name is of the request name
            foundChild = (T)child;
            break;
         }
      }
      else
      {
         // child element found.
         foundChild = (T)child;
         break;
      }
   }

     return foundChild;
  }

这篇关于查找逻辑子节点,包括隐藏节点和折叠节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆