您的位置:首页 > 其它

win8和win8.1动态绑定数据到ListView

2015-06-04 11:47 375 查看
public string stuName { get; set; }
public int stuAge { get; set; }
}

然后就是new一个Student对象,往里面添加值了,这边又有两种方法:
一种是类似java的方法(不知道java是不是有更简便的方法,这种方法个人感觉有点麻烦):

Student stu = new Student { stuName = "钢铁侠", stuAge = 40 };
Student stu1 = new Student { stuName = "蜘蛛侠", stuAge = 40 };
Student stu2 = new Student { stuName = "蝙蝠侠", stuAge = 40 };
Student stu3 = new Student { stuName = "绿巨人", stuAge = 40 };
Student stu4 = new Student { stuName = "黑寡妇", stuAge = 40 };
Student stu5 = new Student { stuName = "美国队长", stuAge = 40 };

List<Student> stuList = new List<Student>();//泛型,这个也是java中的写法
stuList.Add(stu);
stuList.Add(stu1);
stuList.Add(stu2);
stuList.Add(stu3);
stuList.Add(stu4);
stuList.Add(stu5);

还有一个是C#的写法:
List<Student> stuList = new List<Student>
{
new Student{ stuName = "钢铁侠", stuAge = 40 },
new Student{ stuName = "蜘蛛侠", stuAge = 40 },
new Student{ stuName = "蝙蝠侠", stuAge = 40 },
new Student{ stuName = "黑寡妇", stuAge = 40 },
new Student{ stuName = "绿巨人", stuAge = 40 }
};

以上的不同写法都完成了一个目的,就是给stuList赋值了。这也 解决了第二个问题,数据从哪里来的问题。
最后我们就是要完成绑定的步骤了。这个只需要一行代码就能把数据送到前台的ListView让其显示。 这样就完成了最后一个问题,怎样绑定的问题。

注意:这里的 listBind 就是前台的ListView的唯一指定。

listBind.ItemsSource = stuList;

运行程序,就能看到ListView里面有值了,如下图所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: