我需要在WPF控件层次结构中搜索与给定名称或类型匹配的控件。我该怎么做呢?


当前回答

对代码的扩展。

增加了重载,以按类型、按类型和标准(谓词)查找一个子元素,查找符合标准的所有类型的子元素 FindChildren方法除了是DependencyObject的扩展方法外,还是一个迭代器 FindChildren还遍历逻辑子树。请参阅博客中链接的Josh Smith的文章。

来源: https://code.google.com/p/gishu-util/source/browse/#git%2FWPF%2FUtilities

解释性博客文章: http://madcoderspeak.blogspot.com/2010/04/wpf-find-child-control-of-specific-type.html

其他回答

下面是我的代码,通过类型来查找控件,同时控制我们进入层次结构的深度 (maxDepth == 0表示无限深)。

public static class FrameworkElementExtension
{
    public static object[] FindControls(
        this FrameworkElement f, Type childType, int maxDepth)
    {
        return RecursiveFindControls(f, childType, 1, maxDepth);
    }

    private static object[] RecursiveFindControls(
        object o, Type childType, int depth, int maxDepth = 0)
    {
        List<object> list = new List<object>();
        var attrs = o.GetType()
            .GetCustomAttributes(typeof(ContentPropertyAttribute), true);
        if (attrs != null && attrs.Length > 0)
        {
            string childrenProperty = (attrs[0] as ContentPropertyAttribute).Name;
            foreach (var c in (IEnumerable)o.GetType()
                .GetProperty(childrenProperty).GetValue(o, null))
            {
                if (c.GetType().FullName == childType.FullName)
                    list.Add(c);
                if (maxDepth == 0 || depth < maxDepth)
                    list.AddRange(RecursiveFindControls(
                        c, childType, depth + 1, maxDepth));
            }
        }
        return list.ToArray();
    }
}

虽然我一般喜欢递归,但在c#中编程时,它不如迭代有效,所以也许下面的解决方案比John Myczek建议的更整洁?这将从给定控件搜索层次结构,以查找特定类型的祖先控件。

public static T FindVisualAncestorOfType<T>(this DependencyObject Elt)
    where T : DependencyObject
{
    for (DependencyObject parent = VisualTreeHelper.GetParent(Elt);
        parent != null; parent = VisualTreeHelper.GetParent(parent))
    {
        T result = parent as T;
        if (result != null)
            return result;
    }
    return null;
}

像这样调用它来找到包含一个名为ExampleTextBox的控件的窗口:

Window window = ExampleTextBox.FindVisualAncestorOfType<Window>();

您可以使用VisualTreeHelper来查找控件。下面是一个使用VisualTreeHelper查找指定类型的父控件的方法。您也可以使用VisualTreeHelper以其他方式查找控件。

public static class UIHelper
{
   /// <summary>
   /// Finds a parent of a given item on the visual tree.
   /// </summary>
   /// <typeparam name="T">The type of the queried item.</typeparam>
   /// <param name="child">A direct or indirect child of the queried item.</param>
   /// <returns>The first parent item that matches the submitted type parameter. 
   /// If not matching item can be found, a null reference is being returned.</returns>
   public static T FindVisualParent<T>(DependencyObject child)
     where T : DependencyObject
   {
      // get parent item
      DependencyObject parentObject = VisualTreeHelper.GetParent(child);

      // we’ve reached the end of the tree
      if (parentObject == null) return null;

      // check if the parent matches the type we’re looking for
      T parent = parentObject as T;
      if (parent != null)
      {
         return parent;
      }
      else
      {
         // use recursion to proceed with next level
         return FindVisualParent<T>(parentObject);
      }
   }
}

这样叫它:

Window owner = UIHelper.FindVisualParent<Window>(myControl);

下面是一个使用灵活谓词的解决方案:

public static DependencyObject FindChild(DependencyObject parent, Func<DependencyObject, bool> predicate)
{
    if (parent == null) return null;

    int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < childrenCount; i++)
    {
        var child = VisualTreeHelper.GetChild(parent, i);

        if (predicate(child))
        {
            return child;
        }
        else
        {
            var foundChild = FindChild(child, predicate);
            if (foundChild != null)
                return foundChild;
        }
    }

    return null;
}

例如,你可以这样称呼它:

var child = FindChild(parent, child =>
{
    var textBlock = child as TextBlock;
    if (textBlock != null && textBlock.Name == "MyTextBlock")
        return true;
    else
        return false;
}) as TextBlock;

对代码的扩展。

增加了重载,以按类型、按类型和标准(谓词)查找一个子元素,查找符合标准的所有类型的子元素 FindChildren方法除了是DependencyObject的扩展方法外,还是一个迭代器 FindChildren还遍历逻辑子树。请参阅博客中链接的Josh Smith的文章。

来源: https://code.google.com/p/gishu-util/source/browse/#git%2FWPF%2FUtilities

解释性博客文章: http://madcoderspeak.blogspot.com/2010/04/wpf-find-child-control-of-specific-type.html