【问题标题】:Binding data to child Listview element in xamarin将数据绑定到 xamarin 中的子 Listview 元素
【发布时间】:2016-06-06 11:21:26
【问题描述】:

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

<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 是子列表视图的对象。并将其定义为父数据源中的一个属性。

我在这里错过了什么?

为 ParentSource 属性实现了 Notifyproperty。

【问题讨论】:

  • 你能告诉我们你想绑定到这个列表的数据吗,我不确定你在这个嵌套列表上做得很好

标签: c# mvvm xamarin viewmodel xamarin.forms


【解决方案1】:

你可以试试这样的:

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

对我有用,希望对你有帮助!

【讨论】:

  • 在 Android 上运行它时出现 listadapter-fail。不过在 UWP 上效果很好。
  • @JoakimM 实际上,这是一种不好的方法。我们应该改用分组ListViewArticle(滚动到 ListViews 部分)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 2018-08-14
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-26
相关资源
最近更新 更多