question

6666666 avatar image
0 Votes"
6666666 asked 6666666 commented

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

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

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

ColeXia-MSFT avatar image
0 Votes"
ColeXia-MSFT answered 6666666 commented

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.


· 3
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.

Is that I am saying wrong?

I created a custom view with a List or ObservationCollection Property.

 public static readonly BindableProperty ValuesProperty= BindableProperty.Create(nameof(ElectricityValues),
             typeof(List<Models.Equipments.TimeAndCurrent>),
             typeof(PoChart),
             new List<Models.Equipments.TimeAndCurrent>(),
              BindingMode.OneWay,
              propertyChanged: OnPropertyChanged);

but when binding to it in xaml . when I add or remove items from the Items which is binded to the Values it is not changed.

0 Votes 0 ·

Try to change the type from List<Models.Equipments.TimeAndCurrent> to ObservableCollection<Models.Equipments.TimeAndCurrent> .

0 Votes 0 ·

It is not work.

0 Votes 0 ·