I
I
Impeeeery2017-10-08 08:32:31
WPF
Impeeeery, 2017-10-08 08:32:31

How to make a StackPanel inside a ScrollViewer in WPF (or how else to add a load more button at the bottom of the elements in the ListView)?

This is how I do it:

<ListView.Style>
                                                    <Style TargetType="ListView">
                                                                <Setter Property="Template">
                                                                    <Setter.Value>
                                                                        <ControlTemplate TargetType="ListView">

                                                                            <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
                                                                                <StackPanel>
                                                                                    <ItemsPresenter />
                                                                                    <Button Content="More" Name="moreButton"></Button>
                                                                                </StackPanel>
                                                                            </ScrollViewer>
                                                                        </ControlTemplate>
                                                                    </Setter.Value>
                                                                </Setter>
                                                    </Style>
                                                </ListView.Style>

but the ScrollViewer starts behaving inappropriately. scrolling does not work smoothly on one item, but perceives the entire stackpanel as a whole, skipping all the elements.
if instead of a StackPanel there is a Grid with a fixed Height, then this will not happen.
but Grid does not suit me, since the height of the ItemsPresenter, of course, is not known in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-10-08
@petermzg

It is not clear from the question that your button should also scroll or should it be fixed there at the bottom of the control?

<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
  <!-- Задаете шаблон отображения -->
  <ScrollViewer.Template>
     <ControlTemplate TargetType="{x:Type ScrollViewer}">
          <Grid>
             <ScrollContentPresenter /> <!-- Это область куда помещаются элементы, что будут скролировать -->
             <!-- Вместо этого комментария можно поместить кнопку, если не нужно, чтобы она скролировалась -->
             <ScrollBar x:Name="PART_VerticalScrollBar" Value="{TemplateBinding VerticalOffset}" Maximum="{TemplateBinding ScrollableHeight}"
                                   ViewportSize="{TemplateBinding ViewportHeight}" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                   HorizontalAlignment="Right" Style="{DynamicResource ResourceKey=ScrollBarStyle}" />
                    </Grid>
       </ControlTemplate>
   </ScrollViewer.Template>
   <!-- А здесь помещаете все, что должно скролироваться -->
</ScrollViewer>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question