2

I am trying to create a List view control in Xamarin Forms. The list view contains another listview in its datatemplate. Data is getting binded from the viewmodel. But on first go the inner list view is not showing any data. But will show data if tried to scroll the list.

<ListView ItemSource="{Binding ParentSource}">
<ListView.ItemTemplate >
            <DataTemplate>
              <ViewCell>
                <ViewCell.ContextActions>
                  <MenuItem Text="Delete" />
                </ViewCell.ContextActions>
                <ViewCell.View>
                   <Label Text="{Binding Prop1}"/>
                    <ListView ItemSource="{Binding ChildSource}">
                      <ListView.ItemTemplate >
                      <DataTemplate>
                        <ViewCell>
                          <ViewCell.View>
                             <Label Text="{Binding ChildProp1}"/>
                          </ViewCell.View>
                        </ViewCell>
                      </DataTemplate>
                    </ListView.ItemTemplate>
                    </ListView>
               </ViewCell.View>
             </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
</ListView>

The parentsource is the data source object for the parent list. Childsource is the object for child list view. And its defined as a propery in side the parentdatasource.

what am I missing here?

Notifyproperty is implemented for the ParentSource property.

1
  • Could you show us the data you want to bind to this list, I am not sure that your are doing great with this nested lists Commented Feb 24, 2016 at 9:15

1 Answer 1

2

You could try something like this:

<ListView ItemSource="{Binding ParentSource}">
                                  <ListView.ItemTemplate >
                                    <DataTemplate>
                                      <ViewCell>
                                        <ViewCell.ContextActions>
                                          <MenuItem Text="Delete" />
                                        </ViewCell.ContextActions>
                                        <ViewCell.View>
                                          <Label Text="{Binding Prop1}"/>
                                          <ListView ItemSource="{Binding ChildSource}">
                                            <ListView.ItemTemplate >
                                              <DataTemplate>
                                                <ViewCell>
                                                  <ViewCell.View>
                                                    <Grid x:Name="RootGrid"
                                                          BindingContext="{Binding}">
                                                      <Label Text="{Binding BindingContext.ChildProp1, Source={x:Reference RootGrid}}"/>
                                                    </Grid>
                                                  </ViewCell.View>
                                                </ViewCell>
                                              </DataTemplate>
                                            </ListView.ItemTemplate>
                                          </ListView>
                                        </ViewCell.View>
                                      </ViewCell>
                                    </DataTemplate>
                                  </ListView.ItemTemplate>
                                </ListView>

It worked for me, hope it will help you!

Sign up to request clarification or add additional context in comments.

2 Comments

I get a listadapter-fail when running this on Android. Works great on UWP though.
@JoakimM Actually, it's a bad approach. We should use grouped ListView instead. Article (Scroll to ListViews section).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.