萌新求助,WPF用户控件如何使用泛型类? RRS feed


  • 快被这问题折磨死了,恳请各位不吝赐教.

    Hi  杨_123456,



    一个比较常用的泛型如下: 在一个父类容器中找到指定类型控件的集合。

     public static List<T> GetLogicalChildCollection<T>(object parent) where T : DependencyObject
                List<T> logicalCollection = new List<T>();
                GetLogicalChildCollection(parent as DependencyObject, logicalCollection);
                return logicalCollection;
            private static void GetLogicalChildCollection<T>(DependencyObject parent, List<T> logicalCollection) where T : DependencyObject
                IEnumerable children = LogicalTreeHelper.GetChildren(parent);
                foreach (object child in children)
                    if (child is DependencyObject)
                        DependencyObject depChild = child as DependencyObject;
                        if (child is T)
                            logicalCollection.Add(child as T);
                        GetLogicalChildCollection(depChild, logicalCollection);

    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    • 已建议为答案 Misty Zi 2019年4月26日 3:40
    2019年4月22日 5:36