在listview Xamarin表单中绑定自定义项目列表

时间:2017-01-10 16:05:20

标签: c# listview xamarin xamarin.forms xamarin.forms.listview

我正在尝试使用Xamarin表单中的listview,这是我的代码

<StackLayout x:Name="stackMap" Grid.Row="1">
        <Label Text="Seleziona cantiere" Margin="7,0,0,0" TextColor="#3880c4" HorizontalTextAlignment="Start" FontSize="Medium" />
        <ListView x:Name="listView">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                  <StackLayout>
                    <!--<Image Source="{Binding image}" />-->
                    <Label Text="{Binding Name}" TextColor="#f35e20" />
                    <Label Text="{Binding Type}" TextColor="#503026" />
                  </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
      </StackLayout>

和load方法中的代码隐藏:

var cities = new List<VeggieViewModel>();
        cities.Add(new VeggieViewModel { Name = "okkk", Type = "okkk", Image = "okkkk" });
        cities.Add(new VeggieViewModel { Name = "okkk", Type = "okkk", Image = "okkkk" });
        cities.Add(new VeggieViewModel { Name = "okkk", Type = "okkk", Image = "okkkk" });
        cities.Add(new VeggieViewModel { Name = "okkk", Type = "okkk", Image = "okkkk" });
        cities.Add(new VeggieViewModel { Name = "okkk", Type = "okkk", Image = "okkkk" });

        listView.ItemsSource = cities;

类对象:

 public class VeggieViewModel
    {
        public string Name;
        public string Type;
        public string Image;
    }

我的问题是它没有在列表视图中打印任何内容,我正确打印并生成其内部项目的列表视图,但不打印任何内容。 我在哪里做错了?

对不起我分散注意力的错误:( 解决方案是放置{get; set;}对象类。 谢谢你们。 我仍然把这个问题留给像我这样分心的人:)

2 个答案:

答案 0 :(得分:0)

添加{get; set;};这个解决方案有效:

 public class VeggieViewModel
    {
        public string Name {get; set;}
        public string Type {get; set;}
        public string Image {get; set;}
    }

答案 1 :(得分:0)

 public class VeggieViewModel
{
    public string Name {get; set;}
    public string Type {get; set;}
    public string Image {get; set;}
}