【问题标题】:Is there a way to have 2 different objects following one another in a CollectionView in Xamarin.Forms?有没有办法在 Xamarin.Forms 的 CollectionView 中让 2 个不同的对象相互跟随?
【发布时间】:2021-06-18 15:43:06
【问题描述】:

我有一个 CollectionView,它显示了 Categories 对象的列表,如下所示:

我想在它之后列出一个子类别列表,就好像它是同一个列表一样。

我知道 CollectionView 只接受一个项目,但我想知道是否有办法让两个列表在您滚动时可以作为一个列表

感谢您的帮助

【问题讨论】:

  • 选项 1:使用嵌套在集合视图中的 bindable layout(我认为不推荐嵌套集合视图)。选项 2:将模型数据转换为视图模型中的平面列表并绑定。
  • 谢谢@Shaw,但是有没有关于选项 2 的教程,您可以分享一个链接,因为我是 Xamarin.Forms 的新手,我对您所说的内容感到有点困惑?第一个选项是我使用 ItemsSource 为 Category 列表所做的,但似乎第二个选项可能是正确的
  • 选项 1 更适合那些具有多级 UI 的列表。发布您的模型数据代码或有关嵌套列表的一些示例,然后我可以尝试。 Linq 是一个强大的工具。

标签: c# xamarin xamarin.forms xamarin.forms.collectionview


【解决方案1】:

您可以一个接一个地使用 2 个 StackLayout 并设置 BindableLayout 像这样:

<StackLayout Orientation="Horizontal">
      <StackLayout Orientation="Horizontal" BindableLayout.ItemsSource="{Binding CategoryItemSource}">
          <BindableLayout.ItemTemplate>
              <DataTemplate>
                    <!--CategoryItemTemplate-->
                </DataTemplate>
          </BindableLayout.ItemTemplate>
      </StackLayout>
      <StackLayout Orientation="Horizontal" BindableLayout.ItemsSource="{Binding SubCategoryItemSource}">
          <BindableLayout.ItemTemplate>
              <DataTemplate>
                  <!--SubCategoryItemTemplate-->
              </DataTemplate>
          </BindableLayout.ItemTemplate>
      </StackLayout>
    </StackLayout>

在这种情况下,第一个 stacklayout 向您显示类别项目,然后第二个 stacklayout 显示子类别

【讨论】:

  • 我试过了,但没用,它把它们放在另一个上面而不是在同一行
  • 你确定你把父stacklayout的“Orientation”设置为“Horizo​​ntal”和其他stacklayout了
  • 有效!我只需要将它放入 ScrollView 中。非常感谢
【解决方案2】:

两种可能的解决方案:

1- 使用继承使 Category 和 SubCategory 共享一个公共父级,然后建立一个索引,确保每个元素都列在其父级之后。

2- 使用嵌套的集合视图来显示每个类别的子类别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多