UWP C# 列表视图不可滚动
本文关键字:滚动 视图 列表 UWP | 更新日期: 2023-09-27 18:37:08
我有这个列表视图:
<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>
我以单独的方法获取 ItemsSource 调用网络服务的数据(一切正常)
private async void Page_Loading(Windows.UI.Xaml.FrameworkElement sender, object args)
{
entryList.ItemsSource = await webserviceManager.getItems(param1, param2);
}
我的 ListView 也充满了项目,但我无法弄清楚为什么它无法垂直滚动。检查可滚动高度我得到所有 0,所以我认为这些项目是问题所在,到目前为止,控件还没有将它们计为逻辑项目。如果我给 ScrollViewer 一个具体的高度,一切都很好 - 但这不是可行的解决方案,因为我不知道该应用程序稍后将在哪个设备上运行。所以我不知道我还能做什么,也许有人可以帮助我?
编辑:数据源得到了一个
ObservableCollection<entryObject> objlist = new ObservableCollection<NewsObject>();
其中 entryObject 是具有字符串属性的简单数据保存类。
我已经解决了这个问题 - 通过将所有StackPanel更改为具有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>
我还使用基本的 SplitView 更改了我的主页,其中内容面板中的框架调用具有 ListView 的页面。这里有一个 Grid.Row 现在有一个恒定的高度 - 而且,看起来有点奇怪,但现在一切正常。
<SplitView.Content>
<Grid x:Name="mainPagePanel">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
<Button x:Name="HamburgerButton" FontFamily="Segoe MDL2 Assets" Content="" Width="50" Height="50" Background="Transparent" Click="HamburgerButton_Click" Grid.Row="0"/>
<Frame x:Name="viewFrame" Grid.Row="1"></Frame>
</Grid>
</SplitView.Content>
>ListView
已经包含样式中的ScrollViewer
。所以,只需去除你内心的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>