question

XamarinNewBie-6038 avatar image
0 Votes"
XamarinNewBie-6038 asked JarvanZhang-MSFT edited

Xamarin Forms multiply CollectionViews

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>
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.