将对象与子对象绑定困难

时间:2018-12-29 05:30:29

标签: xamarin.forms.listview

我正在使用Xamarin.Forms(C#),正在尝试MVVM方法。

我的课程:

public class Parent
{
   public string FirstName { get; set; }
   public Child Children { get; set; }

   public Parent GetOneParent() {...}
}

public class Child
{
   public string FavoriteFruit { get; set; }
}

首先,这种具有“ compound”属性(即一组孩子)的类称为什么?我不知道这是什么意思,因此我只能在“ Google搜索”中使用它。

好吧,我创建一个父对象:

Parent OneParent = new Parent.GetOneParent();

现在我想在我的XAML代码中显示:

  1. 父母姓名(在标签中)
  2. 孩子们最喜欢的水果的列表(因为有多个,所以在列表视图中)

此类型的对象的标签和列表视图的绑定语法是什么? {绑定???}

1 个答案:

答案 0 :(得分:0)

答案很简单...绑定上下文必须特定于页面,然后更深入地了解其控件。

所以我做了这样的事情...

  1. 在代码背后,我设置了绑定上下文:

    BindingContext =父级;

  2. 然后在XAML代码中...

<StackLayout>
    <Label Text="{Binding FirstName}" />
</StackLayout>
<StackLayout>
    <!-- Now specify a deeper within the Parent object -->
    <ListView x:Name="ParticipantList"
        ItemsSource="{Binding Participants}">

        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell Text="{Binding FavoriteFruit}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

希望对别人有帮助!

相关问题