Reputation: 379
I'm trying to recreate a listview with virtualization to get deeper understanding of how listview works. For this I'm creating a custom panel. When the listview items are arranged vertically I need to know the panel height. Suppose if the items are of same height then I can find the height of panel as ItemsSource.Count * itemHeight. If the items are of varying height how can i set the height of panel without computing the height of all the individual items.
Also when all items are of same height then I can scroll to an item based on the item's index in ItemsSource (ie) index * itemheight will give the position of the item in the panel. In case of varying height how can I find this position. Please guide. Thanks
Upvotes: 1
Views: 94
Reputation: 3024
OK, I understand. It is recommended to use VirtualizingStackPanel
in ItemsPanelTemplate
. After using VirtualizingStackPanel
, it changes from Physical scrolling to Logical scrolling. This will eliminate the effect of height differences.
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Loaded="ItemsStackPanel_Loaded"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
private void ItemsStackPanel_Loaded(object sender, RoutedEventArgs e)
{
var scrollViewer = YourListView.GetScrollViewer();
scrollViewer.VerticalSnapPointsType = SnapPointsType.OptionalSingle;
scrollViewer.ViewChanged += ScrollViewer_ViewChanged; ;
}
private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
{
var elemnt = (sender as ScrollViewer);
if (elemnt != null)
{
Debug.WriteLine($"Scrollable Height : {elemnt.ScrollableHeight} Scrolled Height {elemnt.VerticalOffset}");
}
}
//a sample which scroll into 8 logically
private void Button_Click(object sender, RoutedEventArgs e)
{
var scrollViewer = AuthorGroupsListView.GetScrollViewer();
scrollViewer.ChangeView(null, 8, null, true);
}
Upvotes: 1