我想将分页应用于包含1000条记录的列表框。
答案 0 :(得分:0)
listbox.Listbox没有开箱即用的解决方案,所有wpf控件都适用于视图。如果将数据绑定到列表框,那么显示的是数据视图。您可以轻松构建自己的机制最小化视图上的数据并实现分页控件以根据需要加载项目。
答案 1 :(得分: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;
}
}