如何对列表框wpf应用分页

问题描述 投票:0回答:2

我想对包含1000条记录的列表框应用分页。

.net wpf pagination listbox
2个回答
0
投票

列表框没有开箱即用的解决方案。列表框和所有 wpf 控件都在视图上工作。如果将数据绑定到列表框,则确切显示的是数据的视图。您可以轻松构建自己的机制来最小化视图上的数据并实现分页控件以根据需要加载项目。

结帐http://www.codeproject.com/kb/wpf/wpf_paging.aspx


0
投票

这是我用于列表框简单分页的解决方案。这并不是真正的分页,因为当您点击下一个按钮时,它不会加载每个页面,而是通过页面跳转滚动条来模拟翻页。打开可视化,它可能会满足您对中小型数据集的目的。

XAML:

向上/向下按钮将列表框本身夹在中间。

<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# 代码背后

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.