Xamarin Forms multiply CollectionViews

Xamarin NewBie 121 Reputation points
2021-04-07T02:05:39.987+00:00

I try to use a CollectionView in a other CollectionView like this. My problem is there wont display me the second CV like there dont have a source.
I guess the BindingContex can be the problem but i dont have a idea how i fix it.

      <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>
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,951 Reputation points
    2021-04-07T05:56:05.667+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I guess the BindingContex can be the problem but i dont have a idea how i fix it

    Yes, that is the point. The ItemDateplate's bindingContext is the Model class and is the lower level of the the collectionView's. To achieve the function, try to reset the BindingContext for the item template like : ItemsSource="{Binding BindingContext.DataCollection}" BindingContext="{x:Reference collectionView}"

    Please don't use the nested listVie or collectionView, which may raise the scrolling problem. To display a collection of data in the item template, try using BindableLayout instead.

       <CollectionView x:Name="collectionView" ItemsSource="{Binding DataCollection}">  
           <CollectionView.ItemTemplate>  
               <DataTemplate>  
                       <StackLayout>  
                           <Label Text="{Binding Content}"/>  
                           ...  
                       <StackLayout BindableLayout.ItemsSource="{Binding BindingContext.DataCollection}" BindingContext="{x:Reference collectionView}">  
                               <BindableLayout.ItemTemplate>  
                                   <DataTemplate>  
                                       <StackLayout BackgroundColor="LightBlue">  
                                           <Label Text="{Binding Content}"/>  
                                           ...  
                                       </StackLayout>  
                                   </DataTemplate>  
                               </BindableLayout.ItemTemplate>  
                           </StackLayout>  
                       </StackLayout>  
               </DataTemplate>  
           </CollectionView.ItemTemplate>  
       </CollectionView>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful