Binding data to child Listview element in xamarin

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 15:45:09

问题


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!



来源:https://stackoverflow.com/questions/35597584/binding-data-to-child-listview-element-in-xamarin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!