Forum Xamarin.Forms

How to access and display list of objects inside object in xaml?

jkallunkijkallunki Member ✭✭
edited June 2020 in Xamarin.Forms

These are examples but structure is same which my classes are currently using.

public class Class1 {
    public string Id { get; set; }
    public string Name { get; set; }
    public IList<Class2> Objects { get; set; }
}

public class Class2 {
    public string Id { get; set; }
    public string Name { get; set; }
}

In viewmodel i populate a Class1 object with some data.

private Class1 _object1;
public Class1 Object1
{
    get => _object1;
    set => SetProperty(ref _object1, value);
} 

Now in XAML when I write the following any Class2 objects dont show up.

<StackLayout BindingContext="{Binding Object1}">
    <Entry Text="{Binding Name}"/>
    <ListView ItemsSource="{Binding Objects}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Entry Text="{Binding Name}"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
<StackLayout />

The first entry does display the Name of Object1 but the ListView is empty. I know there is data populated into Object1, checked by looping and logging Object1.Objects.

Tagged:

Best Answers

Answers

  • jkallunkijkallunki Member ✭✭

    I am using Prism as a base. I extend all viewmodels from ViewModelBase. It has the propertynotifier.

        namespace example.ViewModels
        {
            public class ExamplePageViewModel : ViewModelBase
            {
    
            private Class1 _object1;
            public Class1 Object1
            {
                get => _object1;
                set => SetProperty(ref _object1, value);
            }
    
            public ExamplePageViewModel(INavigationService navigationService) : base(navigationService)
            {
    
            }
    
            public override void OnNavigatedTo(INavigationParameters parameters)
                {
                    Object1 = parameters["object1"] as Class1 ;
    
                    foreach (var object2 in Object1.Objects)
                    {
                        Debug.WriteLine(object2.Name);
                    }
                }
        } 
    

    Im getting the data from INavigationParameters passed in the previous viewmodel.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Could you please try to create a small sample reveals the usage and post it here?

  • jkallunkijkallunki Member ✭✭
    edited June 2020
     <StackLayout BindingContext="{Binding Object1}">
                    <Entry Text="{Binding Name}"/>
                    <StackLayout BindableLayout.ItemsSource="{Binding Objects}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <StackLayout>
                                    <Entry Text="{Binding Name}"/>
                                </StackLayout>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </StackLayout >
                <StackLayout />
    

    Interesting. I changed the ListView to StackLayout with BindableLayout and changed IList to ObservableCollection. It seems to display the data now.

    Edit: Also changed ViewCell inside DataTemplate to StackLayout.

  • jkallunkijkallunki Member ✭✭

    Yes ListView works too but leaves empty a lot empty space below last item, even without any items at all.

  • LandLuLandLu Member, Xamarin Team Xamurai

    This is what listview does.
    It will occupy all the rest space of your page.

Sign In or Register to comment.