How to bind to a list or a observationCollection to a custom view?

mc 5,141 Reputation points
2021-03-19T03:27:27.143+00:00

I created a custom view and want to bind a property which is list or observationCollection.

when I add items to the list in viewModel the list in custom view will be changed to .

how to do it?

It seems that it can not use bindableProperty

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-03-19T05:39:25.47+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Firstly take a look at Bindable Layouts in Xamarin.Forms .

    Bindable layouts enable any layout class that derives from the Layout<T> class to generate its content by binding to a collection of items .

    BindableLayout exposes the attached properties (including ItemsSource ,ItemTemplate) .

    The usage is pretty simple

       <StackLayout BindableLayout.ItemsSource="{Binding User.TopFollowers}"  
                    Orientation="Horizontal"  
                    ...>  
           <BindableLayout.ItemTemplate>  
               <DataTemplate>  
                   <controls:CircleImage Source="{Binding}"  
                                         Aspect="AspectFill"  
                                         WidthRequest="44"  
                                         HeightRequest="44"  
                                         ... />  
               </DataTemplate>  
           </BindableLayout.ItemTemplate>  
       </StackLayout>  
    

    Note : While it's technically possible to attach a bindable layout to any layout class that derives from the Layout<T> class, it's not always practical to do so, particularly for the AbsoluteLayout, Grid, and RelativeLayout classes.


    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.