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


264

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

Answers:


311

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

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

Tri Q算法中有一个小错误。找到孩子之后,如果childrenCount> 1并再次进行迭代,我们可以覆盖正确找到的孩子。因此,我if (foundChild != null) break;在代码中添加了a 来处理这种情况。

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

这样称呼它:

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

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


@CrimsonX:也许我做错了...我有一个类似的需求,我需要进入ContentControl(Expander)内的控件(ListBox)。上面的代码对我不起作用。我不得不更新上面的代码,以查看叶节点(GetChildrenCount => 0)是否为ContentControl。如果是,请检查内容是否符合名称+类型条件。
Gishu

@Gishu-我认为它应该为此目的工作。您可以复制并粘贴代码以显示通话使用情况吗?我希望它应该是FindChild <ListBox>(Expander myExpanderName,“ myListBoxName”)。
CrimsonX

3
@CrimsonX我想我发现了另一个极端情况。我试图在RibbonApplicationMenuItem中找到PART_SubmenuPlaceholder,但是上面的代码无法正常工作。要解决它,我需要添加以下内容:if(name == ElementName)else {foundChild = FindChild(child,name)if(foundChild!= null)break; }
kevindaub

6
请小心,答案中有错误或更多。一旦到达搜索类型的子级,它将立即停止。我认为您应该考虑/优先考虑其他答案。
Eric Ouellet 2014年

2
这段代码很棒,但是如果您不查找特定类型的元素(例如,如果您FrameworkElement以T 传递),则它将无法正常工作,一旦第一个循环结束,它将返回null。所以你需要做一些修改。
阿米尔·奥维西斯

131

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

鉴于:

<UserControl ...>
    <TextBlock x:Name="myTextBlock" />
</UserControl>

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

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

当然,由于它是使用x:Name定义的,因此您可以引用生成的字段,但是也许您想动态查找而不是静态查找。

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


6
为此,您不必一定要在name属性中添加“ x:”。
brian buck

3
这似乎并不总是有效。我有UserControls,它们以编程方式组合在嵌套网格中,作为属性窗口的内容。CrimsonX的答案很好用。
马特

4
这不适用于ItemControls,ListBoxes等中的元素。–
Sorensen

67

您可以使用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);

您如何获得或什么是myControl?
Demodave

21

我可能只是在重复其他所有人,但是我确实有一段漂亮的代码,使用方法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;
    }
}

希望你觉得它有用。


2
按照上面我的帖子,有代码中的小错误执行:stackoverflow.com/questions/636383/wpf-ways-to-find-controls/...
CrimsonX

18

我对代码的扩展。

  • 添加了重载以按类型,按类型和条件(谓词)查找一个子代,查找符合条件的所有类型的子代
  • 除了作为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


-1正是我要实现的(谓词,迭代器和扩展方法),但是源链接上有一个404。如果此处包含代码或源链接已固定,则将更改为+1!
cod3monk3y 2014年

@ cod3monk3y - Git的移民被杀似乎:)下面的链接,你去.. code.google.com/p/gishu-util/source/browse/...
Gishu

18

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

    public static IEnumerable<T> FindVisualChildren<T>(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<T>(child))
            {
                yield return other;
            }
        }
    }

3
良好的可确保控件加载,否则GetChildrenCount将返回0
克劳斯NJI

@UrbanEsc,为什么要child第二次投放?如果您有childTypetype T,则可以在ifyield return childType... 内写:
Massimiliano Kraus 2016年

@MassimilianoKraus嘿,很抱歉收到您的回复,但您是对的。我将其归因于我多次重写了此代码段,因此这可能是另一张支票的一部分
UrbanEsc

16

这将消除某些元素-您应该像这样扩展它,以支持更多控件。对于简短的讨论,请在这里看看

 /// <summary>
 /// Helper methods for UI-related tasks.
 /// </summary>
 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 TryFindParent<T>(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<T>(parentObject);
     }
   }

   /// <summary>
   /// This method is an alternative to WPF's
   /// <see cref="VisualTreeHelper.GetParent"/> method, which also
   /// supports content elements. Do note, that for content element,
   /// this method falls back to the logical tree of the element!
   /// </summary>
   /// <param name="child">The item to be processed.</param>
   /// <returns>The submitted item's parent, if available. Otherwise
   /// null.</returns>
   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);
   }
}

5
按照惯例,我希望任何Try*方法都可以返回bool并具有out返回所讨论类型的参数,例如:bool IDictionary.TryGetValue(TKey key, out TValue value)
Drew Noakes

@DrewNoakes那么您建议Philipp称呼它什么呢?同样,即使抱有这样的期望,我仍然发现他的代码既清晰又易于使用。
ANeves 2014年

1
@ANeves,在这种情况下,我就称它为FindParent。对我来说,这个名字意味着它可能会回来null。该Try*前缀在BCL中以我上面描述的方式使用。另请注意,此处的其他大多数答案都使用Find*命名约定。不过,这只是一个小问题:)
Drew Noakes 2014年

16

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

public static T FindChild<T>(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<T>(child, childName);

        if (obj != null)
            return obj;
    }

    return null;
}

1
如果您传递的DependencyObject不是这个方法,则FrameworkElement可能会引发异常。还使用GetChildrenCount上的每一次迭代for像一个坏主意循环的声音。
Tim Pohlmann 2015年

1
好吧,这是从5年前开始的,所以我什至不知道它是否
还能继续

我刚刚提到了它,因为我偶然发现了它,其他也可以找到它;)
Tim Pohlmann

13

虽然我总体上喜欢递归,但在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;
}

这样调用即可找到Window包含名为的控件ExampleTextBox

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

9

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

9

exciton80 ...我遇到了您的代码无法通过用户控件递归的问题。它碰到了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<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;
        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();
}

8

我有一个这样的序列函数(完全通用):

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

获得直系子女:

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

在层级树中查找所有子级:

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

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

收集完之后,可以使用LINQ(即OfType,Where)。


6

由于问题很笼统,可能会吸引人们来寻找非常琐碎的案件的答案:如果您只是想要一个孩子而不是一个后代,则可以使用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();
    }
}

或者显然是遍历Children的循环。


3

这些选项已经讨论过在C#中遍历可视树。也可以使用RelativeSource标记扩展在xaml中遍历可视树。msdn

按类型查找

Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type <TypeToFind>}}}" 

2

这是使用灵活谓词的解决方案:

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;

1

这段代码仅修复了@CrimsonX答案的错误:

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

 // 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
        {
          // child element found.
          foundChild = (T)child;
          break;
        }
      }

      return foundChild;
    }  

如果类型匹配但名称不匹配,您只需要继续递归调用该方法(当您传递FrameworkElementas 时会发生这种情况T)。否则它将返回null,这是错误的。


0

要从代码中查找给定类型的祖先,可以使用:

[CanBeNull]
public static T FindAncestor<T>(DependencyObject d) where T : DependencyObject
{
    while (true)
    {
        d = VisualTreeHelper.GetParent(d);

        if (d == null)
            return null;

        var t = d as T;

        if (t != null)
            return t;
    }
}

此实现使用迭代代替递归,后者可以稍快一些。

如果您使用的是C#7,可以将其缩短一些:

[CanBeNull]
public static T FindAncestor<T>(DependencyObject d) where T : DependencyObject
{
    while (true)
    {
        d = VisualTreeHelper.GetParent(d);

        if (d == null)
            return null;

        if (d is T t)
            return t;
    }
}

-5

试试这个

<TextBlock x:Name="txtblock" FontSize="24" >Hai Welcom to this page
</TextBlock>

背后的代码

var txtblock = sender as Textblock;
txtblock.Foreground = "Red"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.