Finding control within WPF itemscontrol

前端 未结 5 1520
春和景丽
春和景丽 2020-11-27 17:26

Hi i have few a single textbox within the the datatemplate for itemscontrol. When i bind the itemcontrols to a observable collection i get two text boxes. But i need to do s

5条回答
  •  北海茫月
    2020-11-27 17:50

    Thanks Bryce, I tried to tick the up arrow but it says my rating is too low! Sorry!

    I amended the code to return all a list of all the children of the given type as it was what I needed and thought someone else might find it useful.

    Thanks again Bryce, really helpful - sorry about the rating thing!

    public static List FindVisualChildren(DependencyObject depObj) where T : DependencyObject
        {
            List list = new List();
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        list.Add((T)child);
                    }
    
                    List childItems = FindVisualChildren(child);
                    if (childItems != null && childItems.Count() > 0)
                    {
                        foreach (var item in childItems)
                        {
                            list.Add(item);
                        }
                    }
                }
            }
            return list;
        }
    

提交回复
热议问题