win8和win8.1动态绑定数据到ListView
生活随笔
收集整理的這篇文章主要介紹了
win8和win8.1动态绑定数据到ListView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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里面有值了,如下圖所示:
總結
以上是生活随笔為你收集整理的win8和win8.1动态绑定数据到ListView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据绑定的基础
- 下一篇: win8/Metro开发系列二 Xaml