Kar*_* H. 3 c# listview windows-mobile win-universal-app
我有这个列表视图:
<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>
Run Code Online (Sandbox Code Playgroud)
我在单独的方法中获取调用 Web 服务的 ItemsSource 的数据(一切正常)
private async void Page_Loading(Windows.UI.Xaml.FrameworkElement sender, object args)
{
entryList.ItemsSource = await webserviceManager.getItems(param1, param2);
}
Run Code Online (Sandbox Code Playgroud)
我的 ListView 也充满了项目,但我不明白为什么它无法垂直滚动。检查 ScrollableHeight 我总是得到 0,所以我认为这些项目是问题所在,到目前为止,控件并未将它们计为逻辑项目。如果我给 ScrollViewer 一个具体的高度,一切都很好 - 但这不是可行的解决方案,因为我不知道应用程序稍后将在哪个设备上运行。所以我不知道我还能做什么,也许有人可以帮助我?
编辑:数据源有一个
ObservableCollection<entryObject> objlist = new ObservableCollection<NewsObject>();
Run Code Online (Sandbox Code Playgroud)
其中entryObject是一个具有字符串属性的简单数据保存类。
我已经解决了这个问题 - 通过将所有 StackPanels 更改为具有 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>
Run Code Online (Sandbox Code Playgroud)
此外,我还使用基本的 SplitView 更改了 MainPage,其中内容面板中的框架称为带有 ListView 的页面。这里有一个 Grid.Rows 现在具有恒定的高度 - 并且,看起来有点奇怪,但现在一切正常。
<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>
Run Code Online (Sandbox Code Playgroud)