wpf - 查找包含隐藏节点和折叠节点的逻辑子节点

标签 wpf xaml recursion logical-tree

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

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

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

我的功能如下所示:

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

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

我正在绑定(bind)到 xaml 中的一个属性,该属性会关闭并按给定名称查找项目,但该项目当时没有创建,如果我在 xaml 中重新订购该项目,它可以工作并找到该项目。 .. 哦!

最佳答案

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

调用示例:

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

关于wpf - 查找包含隐藏节点和折叠节点的逻辑子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10722791/

相关文章:

c# - 为什么即使设置了 Effects = DragDropEffects.None 也允许放置?

WPF 转换器和 ObservableCollections

wpf - 更改 WPF 中禁用复选框的字体颜色

c# - 捕获 XamlParseException 中丢失的 DLL

java - 如何用这段代码使Java递归?

wpf - 如何通过 WPF Automation API 访问 MessageBox?

c# - 网格中无行和无列子项的奇怪行为

silverlight - Silverlight XAML 中的数据绑定(bind)可为空值未更新

java - 陷入递归循环

javascript - 在javascript中创建N个嵌套循环的策略