wpf - 我可以为 WPF UserControl 创建一个可以返回其控件列表的扩展方法吗?

标签 wpf

当控件未通过属性公开公开时,这可能吗?

最佳答案

这很容易实现:(代码来自this SO question)

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

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

关于wpf - 我可以为 WPF UserControl 创建一个可以返回其控件列表的扩展方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5212963/

相关文章:

wpf - 无法从样式的数据触发器更改 ContentControl 的模板 - 数据触发器未触发

wpf - 自定义控件的自动化测试

c# - 如何正确设置我的边框的动画以向右移动

wpf - 使用 DataTemplate 中应用的样式绑定(bind)到 DataGridTemplateColumn?

c# - focusable 如何影响鼠标事件的引发和处理?

c# - 当标签不为空时隐藏按钮

WPF Dockpanel 不会正确对齐

WPF 绑定(bind)语法问题

c# - 在'System.Windows.StaticResourceExtension 上提供值(value)

c# - 绑定(bind)不适用于在 XAML 中创建的 DependencyObject