Binding data to child Listview element in xamarin

2019-07-28 08:29发布

问题:

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:

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!