开发者

ScrollViewer to dynamically fetch new rows when reaching the bottom

I wish to create a scrollviewer which shows its vertical scrollbar regardless of whether it has items to scroll down or not. By doing this, I seek to dynamically fetch more data from a db as the user scrolls towards the bottom. The data is stored in a datagrid (dgGrid), and is wrapped by a scrollviewer (svMain) (see below). The data from the db is retrieved using a a method that triggers on the ScrollViewer.ScrollChanged event.

<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>

The pr开发者_StackOverflow社区oblem is that even though I set the VerticalScrollBarVisibility property of the scrollviewer to visible (and the vertical scrollbar is shown at all times), the scrollbar is disabled and there is no scroll thumb visible whenever the amount of data in the datagrid fits the height of the screen. If i resize the screen to a smaller size, the vertical scrollbar is enabled and I can move the thumb around. However, I want the scrollbar and its thumb to be enabled regardless of whether the the content of the viewport can accommodate its contents or not. Can anyone provide help on this?

Thanks!


I am not expert in WPF, but after reading the post I can suggest you to keep the height of scrollviewer less than its contents, so you will see the vertical scroll bar enabled.


You could play around with the ICollectionView Interface. Every time an IEnumerable is bound to an ItemsControl, a default view is implicitly inserted between the source and target objects. It tracks the current item, has support for sorting, grouping, etc. You could implement an own Collection View that supports loading further items when the last element was reached.


By default DataGrid uses virtualization, thus new elements will only be initialized and rendered etc., when bringing them into view. This will only work, when using the default scroll controls of the DataGrid.

You could do a

select count(*) from yourDbTable

to fill the list with dummy objects that know how to fetch their data.

You could also add an element to the end of the list that will fetch further elements when accessing one of its properties.

The binding and collection changed events should do the rest.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜