【问题标题】:Xamarin Forms multiply CollectionViews Horizontal and VerticalXamarin Forms 将 CollectionViews 水平和垂直相乘
【发布时间】:2021-02-24 10:10:15
【问题描述】:

我尝试像这样在其他 CollectionView 中使用 CollectionView。我的问题是不会向我显示第二份简历,就像没有来源一样。 我猜 BindingContex 可能是问题所在,但我不知道如何解决它。

      <CollectionView 
       SelectionMode="Single"
       ItemsSource="{Binding MainInfo}">

        <CollectionView.ItemsLayout>
            <LinearItemsLayout ItemSpacing="10" Orientation="Vertical" />
        </CollectionView.ItemsLayout>

        <CollectionView.ItemTemplate>
            <DataTemplate>

                <StackLayout>

                    <Label Text="{Binding UserName}"/>

                    <!--Horizontal CV-->
                    <CollectionView 
                        SelectionMode="Single"
                        ItemsSource="{Binding MainInfo}"
                        >
                        <CollectionView.ItemsLayout>
                            <LinearItemsLayout ItemSpacing="10" Orientation="Horizontal" />
                        </CollectionView.ItemsLayout>

                        <CollectionView.ItemTemplate>
                            <DataTemplate>

                                <StackLayout>

                                    <Label Text="{Binding ImageUrl}"/>
                                    <Label Text="{Binding FullName}"/>

                                </StackLayout>

                            </DataTemplate>
                        </CollectionView.ItemTemplate>

                    </CollectionView>

                </StackLayout>

            </DataTemplate>
        </CollectionView.ItemTemplate>

    </CollectionView>

【问题讨论】:

  • 像这样嵌套简历是个坏主意,你应该避免它
  • 好的,谢谢,达到相同结果的更好方法是什么?

标签: xaml xamarin collectionview


【解决方案1】:

我猜 BindingContext 可能是问题,但我不知道如何解决它。

在您的情况下,ItemsSource 和模型应如下所示

    public class MyDetails
    {
        public string ImageUrl { get; set; }
        public string FullName { get; set; }
    }

    public class MyObject
    {
        public string UserName { get; set; }
        public ObservableCollection<MyDetails> MainInfo { get; set; }
    }

    public class MyViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<MyObject> MainInfo { get; set; }

        protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));        
        }

        
    }

另外,同意@Jason。在您的情况下,如果想在单元格中显示一个集合,您可以使用 Bindable Layouts 而不是将 CollectionView 放在另一个中。这样你只需要像上面的代码一样设置Viewmodel和model即可。

【讨论】:

  • 谢谢@Lucas Bindable Layouts 这是我搜索的内容,但我仍然无法在垂直集合中使用水平集合。我需要像 instagramm 一样的东西
  • 设置 StackLayout 的方向
  • 我怎么能接受?我是 stackoverlow 的新手
  • 点击本回答左上角的“✔”
  • 我完全忽略了这个符号
猜你喜欢
  • 2021-11-08
  • 1970-01-01
  • 2015-02-10
  • 2013-12-10
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2011-11-08
  • 2013-08-12
相关资源
最近更新 更多