c# - 将数据绑定(bind)到 xamarin 中的子 Listview 元素

标签 c# mvvm xamarin viewmodel xamarin.forms

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

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

我在这里想念什么?

为 ParentSource 属性实现了 Notifyproperty。

最佳答案

你可以尝试这样的事情:

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

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

关于c# - 将数据绑定(bind)到 xamarin 中的子 Listview 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597584/

相关文章:

mvvm - ViewCell 未绑定(bind)

c# - DateTime 函数之间有什么区别?

c# - 等同于 C# 异步套接字中的 Peek?

wpf - Josh Smith 在 MVVM 中的 CommandViewModel,有什么意义?

wpf - 在没有绑定(bind)的情况下通过 DataContext 从 ViewModel 获取值(value)?

ios - Xamarin iOS WKWebView 不显示谷歌recaptcha

android - Xamarin Forms ToolBarItem(图标上的数字)通知

c# - Xamarin.Forms:C# 代码与 XAML 代码

c# - 在 AutoPostBack = true 上触发 jQuery 代码?

c# - 反射以查找实现具有多个开放通用参数的接口(interface)的类