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


当前回答

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

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

其他回答

这将消除一些元素-您应该像这样扩展它,以支持更广泛的控件数组。简单的讨论,请看这里

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

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

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;

这里有一些我经常使用的方法。

用法:

// Starts the search from thisUiElement (might be a UserContol, Window, etc..)
var combobox = thisUiElement.ChildOfType<ComboBox>();
var employeesListBox = thisUiElement.ChildOfName("EmployeesListBox");
// Starts the search from MainWindow to find the first DataGrid
var dataGrid = WpfUtils.ChildOfType<DataGrid>();
// Starts the search from MainWindow to find the all ListViews
List<ComboBox> allListViews = WpfUtils.ChildOfType<ListView>();
// Starts the search from MainWindow to find the element of name EmployeesComboBox
var combobox = WpfUtils.ChildOfName("EmployeesComboBox");

实现:

/*
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

namespace WpfUtilities;
*/

public static class WpfUtils{

    public static Window AppMainWindow =>
        Application.Current?.MainWindow;

    #region Find By Type
    
    // Start the search from MainWindow, example usage: var combobox = WpfUtils.ChildOfType<ComboBox>();
    public static T ChildOfType<T>() where T : DependencyObject =>
        ChildOfType<T>(AppMainWindow);
        
    /// This will return the first child of type T
    public static T ChildOfType<T>(this DependencyObject parent)
        where T : DependencyObject
    {
        if (parent == null) return null;
        T child = default;
        var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; i < numVisuals; i++)
        {
            var v = VisualTreeHelper.GetChild(parent, i);
            child = v as T ?? v.ChildOfType<T>();
            if (child != null)
                break;
        }

        return child;
    }
    
    // Start the search from MainWindow, example usage: List<ComboBox> comboboxes = WpfUtils.ChildOfType<ComboBox>();
    public static IEnumerable<T> ChildrenOfType<T>() where T : DependencyObject =>
        ChildrenOfType<T>(AppMainWindow);
    
    /// This will not break the search when finding the first kid of type T, but it will keep searching to return all kids of type T
    public static IEnumerable<T> ChildrenOfType<T>(
        this DependencyObject parent) where T : DependencyObject
    {
        if (parent == null) yield break;
        var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; i < numVisuals; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is T dependencyObject)
                yield return dependencyObject;

            foreach (var childOfChild in child.ChildrenOfType<T>())
                yield return childOfChild;
        }
    }
    
    #endregion  

    #region Find By Name
    
    /// If parent is null, the search will start from MainWindow, example usage: var combobox = WpfUtils.ChildOfName("EmployeesCombobox");
    public static FrameworkElement ChildOfName(string childName,
        DependencyObject parent = null)
    {
        parent ??= AppMainWindow;
        object child = null;
        var numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; i < numVisuals; i++)
        {
            var v = VisualTreeHelper.GetChild(parent, i);
            child = v is FrameworkElement f && f.Name == childName
                ? f
                : ChildOfName(childName, v);

            if (child != null)
                break;
        }

        return child as FrameworkElement;
    }
    
    #endregion
    
    #region
    
    // Yet another useful method, if you are writing code in a .xaml.cs file and you want to get the parent of a type.. example usage: this.ParentOfType<Grid>(); this.ParentOfType<UserControl>(); this.ParentOfType<Window>(); 
    public static T ParentOfType<T>(this DependencyObject child) where T : DependencyObject
    {
        var parentDepObj = child;
        do
        {
            parentDepObj = VisualTreeHelper.GetParent(parentDepObj);
            if (parentDepObj is T parent) return parent;
        } while (parentDepObj != null);

        return null;
    }
    
    #endregion
}

这些选项已经讨论了如何在c#中遍历可视化树。 使用RelativeSource标记扩展也可以遍历xaml中的可视树。msdn

按类型查找

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

我编辑了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;
}