首页 > 解决方案 > 我的 ListView 没有正确显示结果

问题描述

我的 ListView 麻烦的第二版 :( 这是我的代码:

   public class Person
    {
        public string ID { get; set; }
        public string NAME { get; set; }
        public string CODE { get; set; }
        public string CITY { get; set; }

    }     
    public List<Person> CL = new List<Person>() {
     new Person() { ID = "kkkkkkk", NAME = "dsdsdsd", CODE = "sdsdsd", CITY = "sdsdsdsd" },
     new Person() { ID = "kkkkkkk", NAME = "dsdsdsd", CODE = "sdsdsd", CITY = "sdsdsdsd" },
     new Person() { ID = "kkkkkkk", NAME = "dsdsdsd", CODE = "sdsdsd", CITY = "sdsdsdsd" },
     new Person() { ID = "kkkkkkk", NAME = "dsdsdsd", CODE = "sdsdsd", CITY = "sdsdsdsd" },
            };

这是我的xml:

    <ListView ItemsSource="{Binding CL }" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label  Text="{Binding ID}"></Label>
                        <Label Text="{Binding NAME}"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

它在我的页面上没有显示任何内容。
但是当我使用它时,一切正常:

 public ObservableCollection<string> trials1 { get; set; } = new ObservableCollection<string> { "One","More","Character" };

我的xaml在这里:

<ListView  ItemsSource="{Binding trials1 }" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label  Text="{Binding .}"></Label>
                        <Label Text="{Binding Length}"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView> 

标签: c#xamlxamarin.forms

解决方案


您只能绑定到公共属性

这不是一个属性 - 它没有获取/设置

public List<Person> CL = new List<Person>() {

这是一个属性

public List<Person> CL { get; set; } = new List<Person>() {

推荐阅读