开发者

How to apply paging for a listbox wpf

I want to apply paging to the list box which contains 1000s of records开发者_运维技巧.


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.

checkout http://www.codeproject.com/kb/wpf/wpf_paging.aspx


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜