简体   繁体   中英

UWP C# ListView not scrollable

I have this ListView:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ScrollViewer VerticalScrollBarVisibility="Visible" VerticalScrollMode="Enabled">
        <ListView x:Name="entryList" Width="360">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel BorderBrush="Gray">
                        <TextBlock Text="{Binding title}"></TextBlock>
                        <TextBlock Text="{Binding description}"></TextBlock>
                        <TextBlock Text="{Binding author}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ScrollViewer>
</Grid>

I get my data for ItemsSource calling a webservice in a seperate method (works all fine)

private async void Page_Loading(Windows.UI.Xaml.FrameworkElement sender, object args)
{
    entryList.ItemsSource = await webserviceManager.getItems(param1, param2);
}

My ListView is filled with items as well, but I can't figure out why it isn't able to scroll vertically. Checking the ScrollableHeight I get allways 0 so I think the items are the problem, the control doesn't count them as logical items so far. If I give the ScrollViewer a concrete height all is fine - but thats no viable solution for I don't know on which device the app will run later. So I have no faintest idea what I could do else, maybe somebody can help me?

Edit: the DataSource got a

ObservableCollection<entryObject> objlist = new ObservableCollection<NewsObject>();

where entryObject is a simple data holding class with string properties .

I've solved the Problem - by changing all StackPanels to Grids with RowDefinitions.

    <ListView x:Name="entryList" Width="360">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid BorderBrush="Gray">
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding title}" Grid.Row="0"></TextBlock>
                    <TextBlock Text="{Binding description}" Grid.Row="1"></TextBlock>
                    <TextBlock Text="{Binding author}" Grid.Row="2"></TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Also I changed my MainPage with the basic SplitView, where the frame in the content panel called the page with the ListView. Here has one of the Grid.Rows a constant height now - and, seems a little bit weird, but all is working fine now.

<SplitView.Content>
    <Grid x:Name="mainPagePanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition></RowDefinition>
        <Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click" Grid.Row="0"/>                
        <Frame x:Name="viewFrame" Grid.Row="1"></Frame>
    </Grid>            
</SplitView.Content>

ListView already contains ScrollViewer in Style. So, just remove your inner ScrollViewer .

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView x:Name="entryList" Width="360">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel BorderBrush="Gray">
                    <TextBlock Text="{Binding title}"></TextBlock>
                    <TextBlock Text="{Binding description}"></TextBlock>
                    <TextBlock Text="{Binding author}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM