/// <summary> /// Retrieves the visible containers in a LongListSelector and adds them to <paramref name="items"/>. /// </summary> /// <param name="list">LongListSelector that contains the items.</param> /// <param name="itemsPanel">Direct parent of the items.</param> /// <param name="items">List to populate with the containers currently in the viewport</param> /// <param name="selectContent"> /// Specifies whether to return the container or its content. /// For headers, we can't apply projections on the container directly /// (or everything will go blank), so we will apply them on the /// content instead. /// </param> private static void AddVisibileContainers(LongListSelector list, Canvas itemsPanel, List<KeyValuePair<double, FrameworkElement>> items, bool selectContent) { foreach (DependencyObject obj in itemsPanel.GetVisualChildren()) { ContentPresenter container = obj as ContentPresenter; if (container != null && (!selectContent || (VisualTreeHelper.GetChildrenCount(container) == 1 && VisualTreeHelper.GetChild(container, 0) is FrameworkElement))) { GeneralTransform itemTransform = null; try { itemTransform = container.TransformToVisual(list); } catch (ArgumentException) { // Ignore failures when not in the visual tree break; } Rect boundingBox = new Rect(itemTransform.Transform(new Point()), itemTransform.Transform(new Point(container.ActualWidth, container.ActualHeight))); if (boundingBox.Bottom > 0 && boundingBox.Top < list.ActualHeight) { items.Add( new KeyValuePair<double, FrameworkElement>( boundingBox.Top, selectContent ? (FrameworkElement)VisualTreeHelper.GetChild(container, 0) : container)); } } } }