C#中WPF ListView綁定數據的實例詳解

C#中WPF ListView綁定數據的實例詳解

發佈時間: 2019-03-09 19:29:46 來源: 互聯網 作者: 晨曦888 欄目: C#教程 點擊: 298

 

這篇文章主要介紹了C#中WPF ListView綁定數據的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內容,需要的朋友可以參考下

C#中WPF ListView綁定數據的實例詳解

WPF中ListView用來顯示數據十分方便, 我們可以將它分成幾個列,每一個列用來顯示一條數據,但是又是在一方之中。

 

那麼怎樣實現這樣的效果的呢,這就要用綁定了。

我們先來看一看他的xmal代碼


 
  1. <ListView Name="receiveList" Grid.Row="0">
  2. <ListView.View>
  3. <GridView>
  4. <GridView.Columns>
  5. <GridViewColumn Header="發件人"
  6. Width="200"
  7. DisplayMemberBinding="{Binding Path=Senderuser}" />
  8. <GridViewColumn Header="主題"
  9. Width="350"
  10. DisplayMemberBinding="{Binding Path=Topic}" />
  11. <GridViewColumn Header="附件" DisplayMemberBinding="{Binding Path=Ffile}"
  12. Width="200" />
  13. <GridViewColumn Header="時間" Width="150" DisplayMemberBinding="{Binding Path=Time}"/>
  14. </GridView.Columns>
  15. </GridView>
  16. </ListView.View>
  17. </ListView>

上面的代碼中每一個GridViewColumn都有一個綁定{Bind Path=作爲綁定源的類中的成員屬性}

下面來看一下綁定的類


 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace EmailClient
  9. {
  10. class MailList : INotifyPropertyChanged
  11. {
  12. public string senduser;
  13. public string topic;
  14. public string file;
  15. public string time;
  16. public event PropertyChangedEventHandler PropertyChanged;
  17. public string Senderuser
  18. {
  19. get
  20. {
  21. return senduser;
  22. }
  23. set
  24. {
  25. senduser = value;
  26. if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
  27. {
  28. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
  29. }
  30. }
  31. }
  32.  
  33. public string Topic
  34. {
  35. get
  36. {
  37. return topic;
  38. }
  39. set
  40. {
  41. topic = value;
  42. if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
  43. {
  44. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
  45. }
  46. }
  47. }
  48.  
  49. public string Ffile
  50. {
  51. get
  52. {
  53. return file;
  54. }
  55. set
  56. {
  57. file = value;
  58. if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
  59. {
  60. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
  61. }
  62. }
  63. }
  64.  
  65. public string Time
  66. {
  67. get
  68. {
  69. return time;
  70. }
  71. set
  72. {
  73. time = value;
  74. if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
  75. {
  76. this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
  77. }
  78. }
  79. }
  80.  
  81. public MailList() { }
  82. public MailList(string senduser,string topic,string file,string time)
  83. {
  84. this.senduser = senduser;
  85. this.topic = topic;
  86. this.file = file;
  87. this.time = time;
  88. }
  89. }
  90. }
  91.  

現在我們可以看到我們剛纔綁定的屬性就在這個類中,那麼該怎樣應用呢

下面來看一下我的應用代碼


 
  1. private List<MailList> maillist;

 
  1. maillist = new List<MailList>();

以上的代碼是聲明一個list來保存我們插入的數據的,由於我的源代碼是從服務器中得到的郵件列表。


 
  1. maillist.Add(new MailList("xxxxxx", "xxxxxxxx", "xxxxxx", "xxxxxx"));

 
  1. receiveList.ItemsSource = maillist;

如果這樣寫那麼那麼上面的途中得到的就是xxxxxx了。

那麼綁定就是這樣了。

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

本文標題: C#中WPF ListView綁定數據的實例詳解

本文地址: http://www.cppcns.com/ruanjian/csharp/205903.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章