/// <summary> /// Gets the ScrollViewer that contains the containers of an /// ItemsControl. /// </summary> /// <param name="control">The ItemsControl.</param> /// <returns> /// The ScrollViewer that contains the containers of an ItemsControl, or /// null if a ScrollViewer could not be found. /// </returns> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="control" /> is null. /// </exception> public static ScrollViewer GetScrollHost(this ItemsControl control) { if (control == null) { throw new ArgumentNullException("control"); } Panel itemsHost = GetItemsHost(control); if (itemsHost == null) { return(null); } // Walk up the visual tree from the ItemsHost to the // ItemsControl looking for a ScrollViewer that wraps // the ItemsHost. return(itemsHost .GetVisualAncestors() .Where(c => c != control) .OfType <ScrollViewer>() .FirstOrDefault()); }