0

I need to have ListView out of a screen and be able to scroll to the first element which is "out of the screen".

When I moved ListView with margin out of screen I am not able to reach the first position.

I have this:

<ListView Grid.IsSharedSizeScope="True">        
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Focusable" Value="false"/>
                    <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50" SharedSizeGroup="First" />
                            <ColumnDefinition Width="150" SharedSizeGroup="Second"/>
                            <ColumnDefinition Width="200" SharedSizeGroup="Third"/>
                            <ColumnDefinition Width="50" SharedSizeGroup="Fourth"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text ="{Binding some1}" />
                        <TextBlock Grid.Column="1" Text="{Binding some2}" />
                        <TextBlock Grid.Column="2" Text="{Binding some3}" />
                        <Button Grid.Column="3" Click="Cancel">X</Button>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

image1

image2

I need to scroll to the first element as you can see in picture 2. Does anyone know how to do it?

Thank you for advice.

Stepan
  • 5
  • 3

1 Answers1

0

Try wrapping the ListView with an ScrollViewer, then set the VerticalScrollBarVisibility to Visible

<ScrollViewer VerticalScrollBarVisibility="Visible">
    <!--The list-->
</ScrollViewer>
Paaksing
  • 432
  • 1
  • 4
  • 17
  • It is not working, I can see the scrollbar but I can't move with it. – Stepan Jan 05 '20 at 21:29
  • @Stepan Try wrapping the existing ScrollViewer with a Grid, maybe the problem is because you dont have a set height and scrollbar can't find the position – Paaksing Jan 05 '20 at 21:33
  • I wrapped Scrollviewer to the grid and tried to set exact height and it is still not working. – Stepan Jan 05 '20 at 22:07
  • @Stepan Sorry, I didn't read your question carefully, so you moved the list with margins right, let me re-evaluate and edit. – Paaksing Jan 05 '20 at 22:27
  • @Stepan Ok, so I think what you want to accomplish is to view the most recent elements and you can scroll back to older elements, in the way you do is actually kinda of not efficient. The direct solution might be rendering each element of your list from last one to first one, and then you can scroll down to the first one. But if you really wants to do as you described in question, you can look up at this link https://stackoverflow.com/questions/8370209/how-to-scroll-to-the-bottom-of-a-scrollviewer-automatically-with-xaml-and-bindin which involves some autoscroll properties – Paaksing Jan 05 '20 at 22:34