I want to apply paging to the list box which contains 1000s of records.
Asked
Active
Viewed 9,121 times
0
-
like paging a gridview in asp.net – Geeth May 16 '11 at 07:01
-
Perhaps have a look here http://stackoverflow.com/questions/854685/how-do-i-build-a-paged-itemscontrol-or-panel-with-a-repeating-header/10542989#10542989 – NoWar May 10 '12 at 22:24
2 Answers
0
Here is a solution I used for simple paging of a Listbox. It's not really true paging as it doesn't load each page when you hit the next button, but rather jump the scrollbar by a page to simulate a page turn. Turn on Visualization and it would probably serve your purpose for small to medium data sets.
XAML:
Up/Down buttons sandwich the Listbox itself.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button x:Name="ReasonUpButton" Height="60" Width="250" Margin="10,3" Click="ReasonUpButton_Click">
<TextBlock Text="^" FontSize="28"/>
</Button>
<ListBox x:Name="reasonsListBox" Grid.Row="1" Loaded="reasonsListBox_Loaded" ItemsSource="{Binding MyViewModels}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.CanContentScroll="True">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontSize="28"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="ReasonDownButton" Grid.Row="2" Height="60" Width="250" Margin="10,3" Click="ReasonDownButton_Click">
<TextBlock Text="v" FontSize="28"/>
</Button>
</Grid>
C# Code Behind
private void reasonsListBox_Loaded(object sender, RoutedEventArgs e)
{
ScrollViewer scrollViewer = GetDescendantByType(reasonsListBox, typeof(ScrollViewer)) as ScrollViewer;
ReasonUpButton.Visibility = System.Windows.Visibility.Collapsed;
if (scrollViewer.ViewportHeight < scrollViewer.ExtentHeight)
{
ReasonDownButton.Visibility = System.Windows.Visibility.Visible;
}
}
private void ReasonUpButton_Click(object sender, RoutedEventArgs e)
{
ScrollViewer scrollViewer = GetDescendantByType(reasonsListBox, typeof(ScrollViewer)) as ScrollViewer;
if (scrollViewer.VerticalOffset > 0)
{
double newOffset = scrollViewer.VerticalOffset - scrollViewer.ViewportHeight;
scrollViewer.ScrollToVerticalOffset(newOffset);
ReasonDownButton.Visibility = System.Windows.Visibility.Visible;
if (newOffset <= 0)
ReasonUpButton.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
ReasonUpButton.Visibility = System.Windows.Visibility.Collapsed;
}
}
private void ReasonDownButton_Click(object sender, RoutedEventArgs e)
{
ScrollViewer scrollViewer = GetDescendantByType(reasonsListBox, typeof(ScrollViewer)) as ScrollViewer;
if (scrollViewer.VerticalOffset + scrollViewer.ViewportHeight < scrollViewer.ExtentHeight)
{
double newOffset = scrollViewer.VerticalOffset + scrollViewer.ViewportHeight;
scrollViewer.ScrollToVerticalOffset(newOffset);
ReasonUpButton.Visibility = System.Windows.Visibility.Visible;
if (newOffset + scrollViewer.ViewportHeight >= scrollViewer.ExtentHeight)
ReasonDownButton.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
ReasonDownButton.Visibility = System.Windows.Visibility.Collapsed;
}
}

Jon Dosmann
- 667
- 7
- 20
0
There is no out of the box solution for listbox.Listbox and all wpf controls works on views.If you bind a data to the listbox,what exactly being displayed is a view of your data.You can easily build your own mechanism to minimize the data on the view and implement a paging control to load the items as necessary.

biju
- 17,554
- 10
- 59
- 95