How to scroll listbox automatically when Scrollviewer present above it has finished scrolling

142 Views Asked by At

I am developing a windows application,

I have a Parent scroll viewer and inside it there is child scrollviewer and a listbox.

I want to know if there is a way to scroll a listbox(which is fixed in the footer of page) automatically as soon as the scrollviewer present above it has ended its scrolling.

Attached below is the reference image.

enter image description here

Please help as I am new to windows development.

Update1:

Adding the below code as basis:

 <ScrollViewer Name="Scroll1">//parent scrollViewer
 <Grid  Name="ScrollContent">
            <Grid.RowDefinitions>
                <RowDefinition Height="500"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
     <ScrollViewer  Name="A" Grid.Row="0" Grid.Column="0">//child scrollViewer
     ---//Code to display Title1
     <Listbox>
     ---
     ---
     </Listbox>
     --
     </ScrollViewer>

     <Grid Name="B" Grid.Row="1" Grid.Column="0">
     ---//Code to display Title2
     <Listbox>
     ---
     ---
     </Listbox>
     </Grid>

 </Grid>
 </ScrollViewer>

Please let me know if there is any workaround.

1

There are 1 best solutions below

2
Olorunfemi Davis On

Here are the steps to take to achieve this:

  1. Get the Height of the ScrollViewer and save in a variable.
    1. Set an event for when the ScrollViewer view changes.
    2. Inside 2 above, run an if statement to check if the ScrollViewer vertical-offset if equal to the Height. I.e if you've scrolled to the end.
    3. Hide/collapse visibility of the ScrollViewer to enable your ListBox scroll.

If you do not want to hide the visibility of the ScrollViewer at all, you must then utilize the flexibility of Grid children properties.

Do these in xaml

  1. Place your ScrollViewer and ListBox inside a Grid.
  2. Define the Vertical-Definitions of the Grid and set the first Row Height as Auto.
  3. Set the second Row Height as *.
  4. Give the ScrollViewer Row 0 and the ListBox (with a fixed height) Row 1.

In c# In the ScrollViewer ScrollChanged event, once the vertical-offset is equal to the Height of the Grid created above, modify the Grid properties by reversing the amount of space they have on the screen. I.e change the Scroll Viewer Row Height to * (fixed) and change the ListBox Row Height to Auto.

Provide feedback on how this goes.

If you have provided some codes to show how you've tried to fix this yourself, perhaps I would have modified the errors in it. But I hope my answer helps.