【问题标题】:ScrollViewer to dynamically fetch new rows when reaching the bottomScrollViewer 在到达底部时动态获取新行
【发布时间】:2011-01-25 10:55:32
【问题描述】:

我希望创建一个滚动查看器,它显示其垂直滚动条,无论它是否有要向下滚动的项目。通过这样做,我试图在用户滚动到底部时从数据库中动态获取更多数据。数据存储在数据网格 (dgGrid) 中,并由滚动查看器 (svMain) 包装(见下文)。使用触发 ScrollViewer.ScrollChanged 事件的方法检索数据库中的数据。

<ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Visible" DockPanel.Dock="Top"  Name="svMain" cal:Message.Attach="[Event ScrollChanged] = [Action ScrollMaster($eventArgs)]">
                        <StackPanel>
                            <Grid Visibility="{Binding Path=IsFormView, Converter={StaticResource booleanToVisibilityConverter}}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="200"/>
                                </Grid.ColumnDefinitions>

                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>

                                <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding MasterDTO.ActiveFlag, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                    <Label Content="Active"/>
                                </StackPanel>

                                <StackPanel Grid.Row="1" Grid.Column="0">
                                    <con:MandatoryLabel Text="Country Id" />
                                    <TextBox Text="{Binding MasterDTO.CountryId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </StackPanel>
                                <StackPanel Grid.Row="1" Grid.Column="1">
                                    <con:MandatoryLabel Text="Currency"/>
                                    <TextBox Text="{Binding MasterDTO.Currency, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                </StackPanel>
                            </Grid>

                            <!-- Grid View -->
                            <DockPanel Visibility="{Binding Path=IsGridView, Converter={StaticResource booleanToVisibilityConverter}}">
                                <DataGrid Name="dgGrid" Style="{StaticResource ReadOnlyDatagrid}" SelectedItem="{Binding Path=MasterDTO, Mode=TwoWay}" ItemsSource="{Binding MasterDTOs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" cal:Message.Attach="[Event SelectionChanged] = [Action GridSelect]">
                                    <DataGrid.Columns>
                                        <DataGridTemplateColumn Header="Active">
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <controls:YesNoLabel Value="{Binding ActiveFlag}" />
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTextColumn Header="Country Id" Binding="{Binding CountryId}"/>
                                        <DataGridTextColumn Header="Currency" Binding="{Binding Currency}"/>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </DockPanel>

                        </StackPanel>
                    </ScrollViewer>

问题是即使我将滚动查看器的 VerticalScrollBarVisibility 属性设置为可见(并且始终显示垂直滚动条),滚动条被禁用,并且只要数据网格中的数据量很大,就没有滚动拇指可见适合屏幕的高度。如果我将屏幕大小调整为更小的尺寸,则启用垂直滚动条并且我可以移动拇指。但是,无论视口的内容是否可以容纳其内容,我都希望启用滚动条及其拇指。有人可以提供帮助吗?

谢谢!

【问题讨论】:

    标签: wpf xaml scrollviewer


    【解决方案1】:

    我不是 WPF 方面的专家,但是在阅读了这篇文章后,我可以建议你保持 scrollviewer 的高度小于其内容,这样你就会看到启用了垂直滚动条。

    【讨论】:

      【解决方案2】:

      您可以使用ICollectionView Interface。每次将 IEnumerable 绑定到 ItemsControl 时,都会在源对象和目标对象之间隐式插入默认视图。它跟踪当前项目,支持排序、分组等。您可以实现自己的集合视图,支持在到达最后一个元素时加载更多项目。

      【讨论】:

        【解决方案3】:

        默认情况下,DataGrid 使用虚拟化,因此只有在将新元素带入视图时才会对其进行初始化和渲染等操作。这仅在使用 DataGrid 的默认滚动控件时才有效。

        你可以做一个

        select count(*) from yourDbTable
        

        用知道如何获取数据的虚拟对象填充列表。

        您还可以在列表末尾添加一个元素,当访问其属性之一时,该元素将获取更多元素。

        绑定和集合更改事件应该完成其余的工作。

        【讨论】:

          猜你喜欢
          • 2013-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-01
          • 2012-06-02
          • 1970-01-01
          • 2014-12-12
          • 1970-01-01
          相关资源
          最近更新 更多