/// <summary> /// Enumerates all the items that are currently visible in an ItemsControl. This /// implementation works for both virtualized and non-virtualized panels. /// </summary> public static IEnumerable <FrameworkElement> GetItemsInView(this ItemsControl itemsControl) { // find the panel that hosts our items - this is 'cached' // using the ItemsControl.Tag property to minimize visual tree // navigation Panel itemsHostPanel = itemsControl.Tag as Panel; if (itemsHostPanel == null) { itemsHostPanel = itemsControl.Descendants <Panel>() .Cast <Panel>() .Where(p => p.IsItemsHost) .SingleOrDefault(); itemsControl.Tag = itemsHostPanel; } VirtualizingStackPanel vsp = itemsHostPanel as VirtualizingStackPanel; if (vsp != null) { // implementation for virtualizing lists return(GetItemsInView(itemsControl, vsp)); } else { // implementation for non-virtualizing lists return(Enumerable.Range(0, itemsControl.Items.Count) .Select(index => itemsControl.ItemContainerGenerator.ContainerFromIndex(index)) .Cast <FrameworkElement>() .Where(container => container.GetRelativePosition(itemsControl).Y + container.ActualHeight > 0) .Where(container => container.GetRelativePosition(itemsControl).Y - container.ActualHeight < itemsControl.ActualHeight)); } }
public CatalogPage() { InitializeComponent(); Loaded += (sender, e) => { ViewModel.CatalogNavigated += ViewModelOnCatalogNavigated; ViewModel.PropertyChanged += ViewModelOnPropertyChanged; var isSearchEnabled = ViewModel.IsSearchEnabled; AppBar.Buttons[0].IsVisible = isSearchEnabled; AppBar.MenuItems[0].IsVisible = ViewModel.CanRefresh; AppBar.Mode = isSearchEnabled ? ApplicationBarMode.Default : ApplicationBarMode.Minimized; ItemsControl.Margin = isSearchEnabled ? new Thickness(24, 48, 24, 72) : new Thickness(24, 48, 24, 24); _scrollViewer = ItemsControl.Descendants <ScrollViewer>().SingleOrDefault(); }; Unloaded += (sender, e) => { ViewModel.CatalogNavigated -= ViewModelOnCatalogNavigated; ViewModel.PropertyChanged -= ViewModelOnPropertyChanged; }; }
private void LocateScrollViewer() { _scrollViewer = _todoList.Descendants <ScrollViewer>() .Cast <ScrollViewer>() .Single(); // allow interactions to perform some action when the ScrollViewer has been located // such as add event handlers ScrollViewerLocated(_scrollViewer); }
public static IEnumerable <FrameworkElement> GetItemsInView(this ItemsControl itemsControl) { VirtualizingStackPanel virtualizingStackPanel = itemsControl.Descendants <VirtualizingStackPanel>().First <DependencyObject>() as VirtualizingStackPanel; int firstVisibleItem = (int)virtualizingStackPanel.VerticalOffset; int visibleItemCount = (int)virtualizingStackPanel.ViewportHeight; for (int index = firstVisibleItem; index <= firstVisibleItem + visibleItemCount + 1; ++index) { FrameworkElement frameworkElement = itemsControl.ItemContainerGenerator.ContainerFromIndex(index) as FrameworkElement; if (frameworkElement != null) { yield return(frameworkElement); } } }
/// <summary> /// Enumerates all the items that are currently visible in am ItemsControl. This implementation assumes /// that a VirtualizingStackPanel is being used as the ItemsPanel. /// </summary> public static IEnumerable <FrameworkElement> GetItemsInView(this ItemsControl itemsControl) { // locate the stack panel that hosts the items VirtualizingStackPanel vsp = itemsControl.Descendants <VirtualizingStackPanel>().First() as VirtualizingStackPanel; // iterate over each of the items in view int firstVisibleItem = (int)vsp.VerticalOffset; int visibleItemCount = (int)vsp.ViewportHeight; for (int index = firstVisibleItem; index <= firstVisibleItem + visibleItemCount + 1; index++) { var item = itemsControl.ItemContainerGenerator.ContainerFromIndex(index) as FrameworkElement; if (item == null) { continue; } yield return(item); } }
public static IEnumerable <FrameworkElement> GetItemsInView(this ItemsControl itemsControl) { var itemsHostPanel = itemsControl.Tag as Panel; if (itemsHostPanel == null) { itemsHostPanel = itemsControl.Descendants <Panel>().Cast <Panel>().FirstOrDefault(p => p.IsItemsHost); itemsControl.Tag = itemsHostPanel; } var vsp = itemsHostPanel as VirtualizingStackPanel; if (vsp != null) { return(GetItemsInView(itemsControl, vsp)); } return(Enumerable.Range(0, itemsControl.Items.Count) .Select(index => itemsControl.ItemContainerGenerator.ContainerFromIndex(index)) .Cast <FrameworkElement>() .Where(c => c.GetRelativePositionIn(itemsControl).Y + c.ActualHeight > 0) .Where(c => c.GetRelativePositionIn(itemsControl).Y - c.ActualHeight < itemsControl.ActualHeight)); }
private static void OnIsPivotAnimatedChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) { ItemsControl list = d as ItemsControl; list.Loaded += (s2, e2) => { // locate the pivot control that this list is within Pivot pivot = list.Ancestors <Pivot>().Single() as Pivot; // and its index within the pivot int pivotIndex = pivot.Items.IndexOf(list.Ancestors <PivotItem>().Single()); bool selectionChanged = false; pivot.SelectionChanged += (s3, e3) => { selectionChanged = true; }; // handle manipulation events which occur when the user // moves between pivot items pivot.ManipulationCompleted += (s, e) => { if (!selectionChanged) { return; } selectionChanged = false; if (pivotIndex != pivot.SelectedIndex) { return; } // determine which direction this tab will be scrolling in from bool fromRight = e.TotalManipulation.Translation.X <= 0; // locate the stack panel that hosts the items VirtualizingStackPanel vsp = list.Descendants <VirtualizingStackPanel>().First() as VirtualizingStackPanel; // iterate over each of the items in view int firstVisibleItem = (int)vsp.VerticalOffset; int visibleItemCount = (int)vsp.ViewportHeight; for (int index = firstVisibleItem; index <= firstVisibleItem + visibleItemCount; index++) { // find all the item that have the AnimationLevel attached property set var lbi = list.ItemContainerGenerator.ContainerFromIndex(index); if (lbi == null) { continue; } vsp.Dispatcher.BeginInvoke(() => { var animationTargets = lbi.Descendants().Where(p => ListAnimation.GetAnimationLevel(p) > -1); foreach (FrameworkElement target in animationTargets) { // trigger the required animation GetAnimation(target, fromRight).Begin(); } }); } ; }; }; }