Delete an instance of an object from Grid.Children?

I have a List<T> with some UserControl s. There is a Grid in the main window, and some of the UserControl will be added to Grid.Children . Now I would like to remove a specific UserControl from this Grid , for example. I would like to do something like this

 layoutRoot.Children.Remove(controlList[1]); 

Is it possible? I only know FindName() and FindResource() , but all UserControl have no names, so I can not use these methods: (

Thanks in advance!

0
source share
1 answer

just an idea to get started, if you know the type of your user control, you can use these methods:

 static T FindVisualChild<T>(Visual parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { var visual = (Visual)VisualTreeHelper.GetChild(parent, i); child = visual as T; if (child == null) child = FindVisualChild<T>(visual); if (child != null) break; } return child; } 
+1
source

Source: https://habr.com/ru/post/1499834/


All Articles