C# WPF:获取下一个/上一个可视对象

C# WPF:获取下一个/上一个可视对象,c#,wpf,C#,Wpf,我有一个堆栈面板,其中包含许多文本框。有没有办法获取下一个/上一个视觉元素 我想要的功能相当类似于jQuery的.next()函数,它可以获取下一个对象。您可以尝试以下方法来枚举可视化树 public static IEnumerable<T> FindVisualChildren<T> (DependencyObject depObj, string childName) where T : DependencyObject { if (depObj != n

我有一个
堆栈面板
,其中包含许多
文本框
。有没有办法获取下一个/上一个视觉元素


我想要的功能相当类似于jQuery的
.next()
函数,它可以获取下一个对象。

您可以尝试以下方法来枚举可视化树

public static IEnumerable<T> FindVisualChildren<T>
(DependencyObject depObj, string childName) where T : DependencyObject
{

    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {

            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            var frameworkElement = child as FrameworkElement;
            if (child != null && frameworkElement.Name == childName)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child, childName))
            {
                yield return childOfChild;
            }
        }
     }
}

您可以尝试以下方法来枚举可视树

public static IEnumerable<T> FindVisualChildren<T>
(DependencyObject depObj, string childName) where T : DependencyObject
{

    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {

            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            var frameworkElement = child as FrameworkElement;
            if (child != null && frameworkElement.Name == childName)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child, childName))
            {
                yield return childOfChild;
            }
        }
     }
}