将数据绑定到xamarin中的子Listview元素 [英] Binding data to child Listview element in xamarin

查看:116
本文介绍了将数据绑定到xamarin中的子Listview元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Xamarin Forms中创建一个List视图控件.列表视图在其数据模板中包含另一个列表视图.数据正在从视图模型绑定.但是首先,内部列表视图不显示任何数据.但是如果尝试滚动列表将显示数据.

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>

parentsource是父级列表的数据源对象. Childsource是子列表视图的对象.并将其定义为parentdatasource旁边的属性.

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.

我在这里想念什么?

Notifyproperty是针对ParentSource属性实现的.

Notifyproperty is implemented for the ParentSource property.

推荐答案

您可以尝试以下方法:

<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!

这篇关于将数据绑定到xamarin中的子Listview元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆