Forum Xamarin.Forms

How to use C# Markup with a CollectionView?

I was checking out how to use the new C# Markup functionality in my Xamarin.Forms app and I tried to implement it in a list.
However upon checking the documentation, I couldn't find an example on how to implement it. So I tried it like this:

Page code, viewModel.Init() just gets the data for technologies:

protected override void OnAppearing()
        {
            base.OnAppearing();
            viewModel.Init();
            Content = new CollectionView
            {
                BackgroundColor = Color.Purple,
                ItemTemplate = new DataTemplate(typeof(TechCardView))
            }.Bind(ItemsView.ItemsSourceProperty, nameof(viewModel.Technologies));
        }

ViewModel code just in case:

public override void Init()
        {
            _ = GetStockTechnologies();
        }

        private async Task GetStockTechnologies()
        {
            Technologies = await _getTechnologies.BuildUseCaseTaskAsync(null);
        }

I thought this would work because Technologies is a IEnumerable of TechObject, and inside TechCardView I have only one property that is of type TechObject, but I couldn't find a way to do a {Binding .} inside DataTemplate like I would in XAML.

This is what I could get with the documentation in C# Markup and the DataTemplate documentation and while I do get data, the list does not show any items.

What's the proper way to do this?

Best Answer

  • albertoha94albertoha94 Member ✭✭✭
    Accepted Answer

    Thanks @JoeManke & @Jarvan for the support.
    Seems the issue was simpler than I thought, I was just missing the PropertyChanged call.

    IEnumerable<TechObject> technologies = new List<TechObject>();
            public IEnumerable<TechObject> Technologies
            {
                get { return technologies; }
                set { SetProperty(ref technologies, value); }
            }
    

    Once I changed my variable like this, the items started displaying, so there's no issue related with the MarkUp or anything.

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited November 2020

    while I do get data, the list does not show any items.

    To set data binding for CollectionView in the code behind, try using the SetBinding method.

    Check the code:

    CollectionView collectionView = new CollectionView();
    collectionView.SetBinding(ItemsView.ItemsSourceProperty, "DataCollection");
    
    collectionView.ItemTemplate = new DataTemplate(() =>
    {
        ...
        return layout;
    });
    
    Content = collectionView;
    
    BindingContext = new CustomViewModel();
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭
    1. Are you setting the BindingContext of the Page to the ViewModel?
    2. Does the ViewModel implement INotifyPropertyChanged correctly?
  • albertoha94albertoha94 Member ✭✭✭
    Accepted Answer

    Thanks @JoeManke & @Jarvan for the support.
    Seems the issue was simpler than I thought, I was just missing the PropertyChanged call.

    IEnumerable<TechObject> technologies = new List<TechObject>();
            public IEnumerable<TechObject> Technologies
            {
                get { return technologies; }
                set { SetProperty(ref technologies, value); }
            }
    

    Once I changed my variable like this, the items started displaying, so there's no issue related with the MarkUp or anything.

Sign In or Register to comment.