Chris Hayes
Chris Hayes

Reputation: 4033

Dynamic CollectionView with FreshMvvm Xamarin.Forms

Is there a way to generate dynamic layouts in collectionview?

I'm familiar with DataTemplateSelector https://www.xamarinhelp.com/xamarin-forms-datatemplateselector/

and I'm familiar with FreshMvvm content by convention.

Can I add objects to an observable collection and have FreshMvvm resolve the content page?

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="trycollections.Pages.MainPage">

    <CollectionView ItemsSource="{Binding MyItems}">
        <CollectionView.ItemsLayout>
            <GridItemsLayout Span="1" Orientation="Vertical" />
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <ContentPresenter Content="{Binding .}"></ContentPresenter>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

</ContentPage>


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace trycollections.ViewModels
{
    public class MainViewModel : FreshMvvm.FreshBasePageModel
    {
        public ObservableCollection<object> MyItems { get; set; } =
            new ObservableCollection<object>
            {
                new ItemOneViewModel(),
                new ItemTwoViewModel(),
                new ItemOneViewModel(),
                new ItemOneViewModel(),
                new ItemOneViewModel(),
            };
    }

    public class ItemOneViewModel : FreshMvvm.FreshBasePageModel
    {
        public string ThisIsOneText { get; set; } = $"Hello world from {nameof(ItemOneViewModel)}";
    }
    public class ItemTwoViewModel : FreshMvvm.FreshBasePageModel
    {
        public string ThisIsTwoText { get; set; } = $"Hello world from {nameof(ItemTwoViewModel)}";
    }


}

Upvotes: 1

Views: 210

Answers (0)

Related Questions