The easiest way is to find all LibraryStackItem or Label under LibraryStack in the visual tree. try it
private void SomeMethod() {
Alternatively, you can get a container for each element and get Label instead
foreach (var element in TaggingContainer.Items) { LibraryStackItem libraryStackItem = TaggingContainer.ItemContainerGenerator.ContainerFromItem(element) as LibraryStackItem; Label label = VisualTreeHelpers.GetVisualChild<Label>(libraryStackItem); }
GetVisualChild
private static T GetVisualChild<T>(DependencyObject parent) where T : Visual { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); child = v as T; if (child == null) { child = GetVisualChild<T>(v); } if (child != null) { break; } } return child; }
GetVisualChildCollection
public static List<T> GetVisualChildCollection<T>(object parent) where T : Visual { List<T> visualCollection = new List<T>(); GetVisualChildCollection(parent as DependencyObject, visualCollection); return visualCollection; } private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual { int count = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < count; i++) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (child is T) { visualCollection.Add(child as T); } else if (child != null) { GetVisualChildCollection(child, visualCollection); } } }
source share