public object ContainerFromItem(object item) { if (RowsListView.ItemsSource is ImageWallRows <Post> rows) { foreach (var row in rows) { if (RowsListView.ContainerFromItem(row) != null) { var container = RowsListView.ContainerFromItem(row) as ListViewItem; if (container != null) { var root = (FrameworkElement)container.ContentTemplateRoot; var innerListView = (ListView)root.FindName("InnerListView"); var c = innerListView.ContainerFromItem(item); if (c != null) { return(c); } } } } } return(null); }
private int getListViewCurrentRowIndex() { var listViewY = getElementPositionY(RowsListView); var minOffset = double.MaxValue; int minOffsetPageIndex = -1; // Return -1 if there is no content loaded in the listview if (RowsListView.Items.Count == 0) { return(-1); } for (int i = 0; i < RowsListView.Items.Count(); i++) { var container = RowsListView.ContainerFromIndex(i); if (container != null) { if (((FrameworkElement)container).ActualHeight != 0) { var y = getElementPositionY((UIElement)container) - listViewY; if (y != -listViewY) { // If the item is virtualized, its position Y will be 0 if (Math.Abs(y) <= minOffset) { minOffset = Math.Abs(y); minOffsetPageIndex = i; } } } } else { // This container is virtualized, Do nothing } } return(minOffsetPageIndex); }
private async void RowsListView_SizeChanged(object sender, SizeChangedEventArgs e) { if (this.DataContext == null) { return; } if ((RowsListView.ItemsSource as IImageWallRows) != null) { if (!resizePending && e.NewSize.Width - this.Padding.Left - this.Padding.Right == (RowsListView.ItemsSource as IImageWallRows).RowWidth) { return; } if (resizePending) { resizePending = false; var temp = e.NewSize; await Task.Delay(500); if (RowsListView.RenderSize.Width == temp.Width) { var currentRow = getListViewCurrentRowIndex(); int currentRowMidItemIndex = 0; if (currentRow != -1) { var t = (RowsListView.ItemsSource as IImageWallRows).t; currentRowMidItemIndex = (RowsListView.ItemsSource as IEnumerable <object>).Take(currentRow).Sum(o => (o as IEnumerable <object>).Count()); currentRowMidItemIndex += ((dynamic)RowsListView.ItemsSource)[currentRow].Count / 2; } (RowsListView.ItemsSource as IImageWallRows).RowWidth = RowsListView.ActualWidth - RowsListView.Padding.Left - RowsListView.Padding.Right; (RowsListView.ItemsSource as IImageWallRows).RowHeight = RowsListView.ActualWidth > 500 ? 300 : 150; (RowsListView.ItemsSource as IImageWallRows).Resize(); if (currentRow != -1) { int sum = 0; foreach (var row in (RowsListView.ItemsSource as IEnumerable <object>)) { sum += (row as IEnumerable <object>).Count(); if (sum >= currentRowMidItemIndex) { RowsListView.ScrollIntoView(row, ScrollIntoViewAlignment.Leading); break; } } } } } else { var temp = e.NewSize; await Task.Delay(500); if (RowsListView.RenderSize.Width == temp.Width) { var currentRow = getListViewCurrentRowIndex(); int currentRowMidItemIndex = 0; if (currentRow != -1) { var t = (RowsListView.ItemsSource as IImageWallRows).t; currentRowMidItemIndex = (RowsListView.ItemsSource as IEnumerable <object>).Take(currentRow).Sum(o => (o as IEnumerable <object>).Count()); if (currentRow + 1 <= (RowsListView.ItemsSource as IList).Count) { var next = (RowsListView.ItemsSource as IEnumerable <object>).Take(currentRow + 1).Sum(o => (o as IEnumerable <object>).Count()); currentRowMidItemIndex = (currentRowMidItemIndex + next) / 2; } } (RowsListView.ItemsSource as IImageWallRows).RowWidth = RowsListView.ActualWidth - RowsListView.Padding.Left - RowsListView.Padding.Right; (RowsListView.ItemsSource as IImageWallRows).RowHeight = RowsListView.ActualWidth > 500 ? 300 : 150; (RowsListView.ItemsSource as IImageWallRows).Resize(); if (currentRow != -1) { int sum = 0; foreach (var row in (RowsListView.ItemsSource as IEnumerable <object>)) { sum += (row as IEnumerable <object>).Count(); if (sum >= currentRowMidItemIndex) { RowsListView.ScrollIntoView(row, ScrollIntoViewAlignment.Leading); break; } } } } } } }