【发布时间】:2019-09-11 06:10:13
【问题描述】:
- 嵌套的 CollectionView 相互滚动:官方支持吗?
- 显示这些集合问题
请看下面我的数据模型和 XAML 代码(我没有网站可以放入生成的屏幕图像)
namespace Notes.Models
{
public class Note
{
public enum NoteStatus { suspended, alive }
public string Description { get; set; }
public NoteStatus Status { get; set; }
}
public class NotesContainer
{
public string Name { get; set; }
public DateTime LastModified { get; set; }
public ObservableCollection<Note> ListOfNotes { get; set; }
}
}
<CollectionView x:Name="notesContainers" SelectionMode="Single" EmptyView="No items currently exist !">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BorderColor="Red" BackgroundColor="Beige" CornerRadius="3" HasShadow="False" Padding="5">
<StackLayout BackgroundColor="Aqua" Padding="5">
<Grid>
<Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/></Grid.RowDefinitions>
<Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
<Label Grid.RowSpan="2" Text="{Binding Name}" VerticalTextAlignment="Center" FontSize="Large"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding LastModified, StringFormat='\{0:dddd dd}'}" HorizontalTextAlignment="End"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding LastModified, StringFormat='\{0:MMMM yyyy}'}" HorizontalTextAlignment="End"/>
</Grid>
<StackLayout BackgroundColor="BlueViolet" Padding="10">
<CollectionView ItemsSource="{Binding ListOfNotes}" SelectionMode="Single" EmptyView="No items currently exist !">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout BackgroundColor="Coral" Padding="0,3">
<Frame BorderColor="Blue" BackgroundColor="LightBlue" CornerRadius="3" HasShadow="False" Padding="5">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Description}" HorizontalOptions="Start" VerticalTextAlignment="Center"/>
<Label Text="{Binding Status}" HorizontalOptions="EndAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>
</StackLayout>
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
外部和内部集合视图的快照:
我强调了颜色以更好地查看未缩小的布局。
问题:(我尝试了多种配置但没有解决方案)
- 如何将 StackLayouts 缩小到其内容?
- 为什么 StackLayouts 会拉长更大的屏幕尺寸?
【问题讨论】:
-
你的 CollectionView 无法显示,因为你没有设置外部 CollectionView 的 ItemSource,如果你嵌套 CollectionView 并在另一个里面滚动,很多时候它会导致滑动冲突
-
外部 CollectionView 的 ItemsSource 是在 XAML 中所见的 x:Name notesContainers 后面的代码中设置的。数据显示正确。问题是 Frames 和/或 StackLayouts 不会缩小到它们的内容。每个项目都沿着屏幕延伸等等。
-
你有没有考虑过使用ListView Grouping,你的需求看起来像两个list,一个是parent,一个是child。参考:docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
-
我第一次看到 ListView 但论坛说嵌套 ListView 存在嵌套滚动姿势问题。因此我希望新的 CollectionView 能够接受嵌套的。这是可能的,因为我看到许多网页在页面本身滚动的内部滚动代码区域滚动!不过,我会回顾一下 DataTemplates 中的布局和 ListView 中的 Group Headers 的可能性。
-
可以,只需要在listview中显示第二个list数据,listview分组就可以实现
标签: android xaml xamarin.forms collectionview