当前位置:  开发笔记 > 编程语言 > 正文

如何按名称或类型查找WPF控件?

如何解决《如何按名称或类型查找WPF控件?》经验,为你挑选了13个好方法。

我需要在WPF控件层次结构中搜索与给定名称或类型匹配的控件.我怎样才能做到这一点?



1> CrimsonX..:

我结合了John Myczek使用的模板格式和上面的Tri Q算法来创建一个可以在任何父级上使用的findChild算法.请记住,向下递归搜索树可能是一个漫长的过程.我只是在WPF应用程序上对此进行了检查,请对您可能发现的任何错误发表评论,我将更正我的代码.

WPF Snoop是查看可视化树的有用工具 - 我强烈建议在测试或使用此算法检查您的工作时使用它.

Tri Q算法中存在一个小错误.找到孩子后,如果childrenCount> 1并且我们再次迭代,我们可以覆盖正确找到的孩子.因此我if (foundChild != null) break;在我的代码中添加了一个来处理这个条件.

/// 
/// Finds a Child of a given item in the visual tree. 
/// 
/// A direct parent of the queried item.
/// The type of the queried item.
/// x:Name or Name of child. 
/// The first parent item that matches the submitted type parameter. 
/// If not matching item can be found, 
/// a null parent is being returned.
public static T FindChild(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(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;
}

像这样称呼它:

TextBox foundTextBox = 
   UIHelper.FindChild(Application.Current.MainWindow, "myTextBoxName");

注意Application.Current.MainWindow可以是任何父窗口.


请小心,答案中有一个或多个错误.一旦它到达搜索类型的孩子,它就会停止.我认为你应该考虑/优先考虑其他答案.
@CrimsonX我想我发现了另一个角落案例.我试图在RibbonApplicationMenuItem中找到PART_SubmenuPlaceholder,但上面的代码不起作用.要解决它,我需要添加以下内容:if(name == ElementName)else {foundChild = FindChild(child,name)if(foundChild!= null)break; }
这段代码很棒,但是如果你没有寻找特定类型的元素,它将无法工作,例如,如果你将`FrameworkElement`作为T传递,它会在第一个循环结束时立即返回null.所以你需要做一些修改.

2> Drew Noakes..:

您还可以使用FrameworkElement.FindName(string)按名称查找元素.

鉴于:


    

在代码隐藏文件中,您可以编写:

var myTextBlock = (TextBlock)this.FindName("myTextBlock");

当然,因为它是使用x:Name定义的,所以你可以只引用生成的字段,但也许你想要动态地而不是静态地查找它.

此方法也适用于模板,其中命名项目多次出现(每次使用模板一次).


为此,您不必将"x:"添加到name属性.
这不适用于ItemControls,ListBoxes等中的元素。
这似乎总是有效.我有UserControls,它们在嵌套网格中以编程方式组合在一起作为属性窗口的内容.但CrimsonX的答案很好.

3> John Myczek..:

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

public static class UIHelper
{
   /// 
   /// Finds a parent of a given item on the visual tree.
   /// 
   /// The type of the queried item.
   /// A direct or indirect child of the queried item.
   /// The first parent item that matches the submitted type parameter. 
   /// If not matching item can be found, a null reference is being returned.
   public static T FindVisualParent(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(parentObject);
      }
   }
}

像这样称呼它:

Window owner = UIHelper.FindVisualParent(myControl);



4> Tri Q Tran..:

我可能只是在重复其他所有人,但我确实有一段很好的代码,它使用FindChild()方法扩展DependencyObject类,它将按类型和名称为您提供子类.只是包括和使用.

public static class UIChildFinder
{
    public static DependencyObject FindChild(this DependencyObject reference, string childName, Type childType)
    {
        DependencyObject foundChild = null;
        if (reference != null)
        {
            int childrenCount = VisualTreeHelper.GetChildrenCount(reference);
            for (int i = 0; i < childrenCount; i++)
            {
                var child = VisualTreeHelper.GetChild(reference, i);
                // If the child is not of the request child type child
                if (child.GetType() != childType)
                {
                    // recursively drill down the tree
                    foundChild = FindChild(child, childName, childType);
                }
                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 = child;
                        break;
                    }
                }
                else
                {
                    // child element found.
                    foundChild = child;
                    break;
                }
            }
        }
        return foundChild;
    }
}

希望你觉得它有用.


根据我上面的帖子,您的代码中存在一个小的实现错误:http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls/1759923#1759923

5> Gishu..:

我对代码的扩展.

添加重载以按类型,按类型和条件(谓词)查找一个子项,查找符合条件的所有类型的子项

除了作为DependencyObject的扩展方法之外,FindChildren方法还是一个迭代器

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



6> UrbanEsc..:

如果要查找特定类型的所有控件,您可能也对此代码段感兴趣

    public static IEnumerable FindVisualChildren(DependencyObject parent) 
        where T : DependencyObject
    {
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);

            var childType = child as T;
            if (childType != null)
            {
                yield return (T)child;
            }

            foreach (var other in FindVisualChildren(child))
            {
                yield return other;
            }
        }
    }


好的,但确保加载控制,否则GetChildrenCount将返回0.

7> 小智..:

这将忽略一些元素 - 您应该像这样扩展它以支持更广泛的控件.有关简短的讨论,请查看此处

 /// 
 /// Helper methods for UI-related tasks.
 /// 
 public static class UIHelper
 {
   /// 
   /// Finds a parent of a given item on the visual tree.
   /// 
   /// The type of the queried item.
   /// A direct or indirect child of the
   /// queried item.
   /// The first parent item that matches the submitted
   /// type parameter. If not matching item can be found, a null
   /// reference is being returned.
   public static T TryFindParent(DependencyObject child)
     where T : DependencyObject
   {
     //get parent item
     DependencyObject parentObject = GetParentObject(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 TryFindParent(parentObject);
     }
   }

   /// 
   /// This method is an alternative to WPF's
   ///  method, which also
   /// supports content elements. Do note, that for content element,
   /// this method falls back to the logical tree of the element!
   /// 
   /// The item to be processed.
   /// The submitted item's parent, if available. Otherwise
   /// null.
   public static DependencyObject GetParentObject(DependencyObject child)
   {
     if (child == null) return null;
     ContentElement contentElement = child as ContentElement;

     if (contentElement != null)
     {
       DependencyObject parent = ContentOperations.GetParent(contentElement);
       if (parent != null) return parent;

       FrameworkContentElement fce = contentElement as FrameworkContentElement;
       return fce != null ? fce.Parent : null;
     }

     //if it's not a ContentElement, rely on VisualTreeHelper
     return VisualTreeHelper.GetParent(child);
   }
}


按照惯例,我希望任何`Try*`方法返回`bool`并有一个`out`参数返回有问题的类型,如:`bool IDictionary.TryGetValue(TKey key,out TValue value)`

8> andresp..:

我编辑了CrimsonX的代码,因为它不适用于超类类型:

public static T FindChild(DependencyObject depObj, string childName)
   where T : DependencyObject
{
    // Confirm obj is valid. 
    if (depObj == null) return null;

    // success case
    if (depObj is T && ((FrameworkElement)depObj).Name == childName)
        return depObj as T;

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

        //DFS
        T obj = FindChild(child, childName);

        if (obj != null)
            return obj;
    }

    return null;
}



9> Nathan Phill..:

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

public static T FindVisualAncestorOfType(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;
}

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

Window window = ExampleTextBox.FindVisualAncestorOfType();



10> 小智..:

这是我的代码,用于按类型查找控件,同时控制我们进入层次结构的深度(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 list = new List();
        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();
    }
}



11> Shawn Nelson..:

exciton80 ...我的代码没有通过usercontrols递归出现问题.它击中了Grid根并抛出错误.我相信这可以解决它:

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 list = new List();
    var attrs = o.GetType().GetCustomAttributes(typeof(ContentPropertyAttribute), true);
    if (attrs != null && attrs.Length > 0)
    {
        string childrenProperty = (attrs[0] as ContentPropertyAttribute).Name;
        if (String.Equals(childrenProperty, "Content") || String.Equals(childrenProperty, "Children"))
        {
            var collection = o.GetType().GetProperty(childrenProperty).GetValue(o, null);
            if (collection is System.Windows.Controls.UIElementCollection) // snelson 6/6/11
            {
                foreach (var c in (IEnumerable)collection)
                {
                    if (c.GetType().FullName == childType.FullName)
                        list.Add(c);
                    if (maxDepth == 0 || depth < maxDepth)
                        list.AddRange(RecursiveFindControls(
                            c, childType, depth + 1, maxDepth));
                }
            }
            else if (collection != null && collection.GetType().BaseType.Name == "Panel") // snelson 6/6/11; added because was skipping control (e.g., System.Windows.Controls.Grid)
            {
                if (maxDepth == 0 || depth < maxDepth)
                    list.AddRange(RecursiveFindControls(
                        collection, childType, depth + 1, maxDepth));
            }
        }
    }
    return list.ToArray();
}



12> 小智..:

我有这样的序列函数(这是完全一般的):

    public static IEnumerable SelectAllRecursively(this IEnumerable items, Func> func)
    {
        return (items ?? Enumerable.Empty()).SelectMany(o => new[] { o }.Concat(SelectAllRecursively(func(o), func)));
    }

立即生孩子:

    public static IEnumerable FindChildren(this DependencyObject obj)
    {
        return Enumerable.Range(0, VisualTreeHelper.GetChildrenCount(obj))
            .Select(i => VisualTreeHelper.GetChild(obj, i));
    }

在层级树上寻找所有孩子:

    public static IEnumerable FindAllChildren(this DependencyObject obj)
    {
        return obj.FindChildren().SelectAllRecursively(o => o.FindChildren());
    }

您可以在Window上调用它来获取所有控件.

收集完成后,可以使用LINQ(即OfType,Where).



13> El Zorko..:

由于问题足够普遍,它可能会吸引人们寻找非常微不足道的案例的答案:如果你只想要一个孩子而不是一个后代,你可以使用Linq:

private void ItemsControlItem_Loaded(object sender, RoutedEventArgs e)
{
    if (SomeCondition())
    {
        var children = (sender as Panel).Children;
        var child = (from Control child in children
                 where child.Name == "NameTextBox"
                 select child).First();
        child.Focus();
    }
}

或者当然是明显的循环迭代儿童.

推荐阅读
我我檬檬我我186
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有