C#中WPF ListView綁定數據的實例詳解
發佈時間: 2019-03-09 19:29:46 來源: 互聯網 作者: 晨曦888 欄目: C#教程 點擊: 298
這篇文章主要介紹了C#中WPF ListView綁定數據的實例詳解的相關資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內容,需要的朋友可以參考下
C#中WPF ListView綁定數據的實例詳解
WPF中ListView用來顯示數據十分方便, 我們可以將它分成幾個列,每一個列用來顯示一條數據,但是又是在一方之中。
那麼怎樣實現這樣的效果的呢,這就要用綁定了。
我們先來看一看他的xmal代碼
- <ListView Name="receiveList" Grid.Row="0">
- <ListView.View>
- <GridView>
- <GridView.Columns>
- <GridViewColumn Header="發件人"
- Width="200"
- DisplayMemberBinding="{Binding Path=Senderuser}" />
- <GridViewColumn Header="主題"
- Width="350"
- DisplayMemberBinding="{Binding Path=Topic}" />
- <GridViewColumn Header="附件" DisplayMemberBinding="{Binding Path=Ffile}"
- Width="200" />
- <GridViewColumn Header="時間" Width="150" DisplayMemberBinding="{Binding Path=Time}"/>
- </GridView.Columns>
- </GridView>
- </ListView.View>
- </ListView>
上面的代碼中每一個GridViewColumn都有一個綁定{Bind Path=作爲綁定源的類中的成員屬性}
下面來看一下綁定的類
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace EmailClient
- {
- class MailList : INotifyPropertyChanged
- {
- public string senduser;
- public string topic;
- public string file;
- public string time;
- public event PropertyChangedEventHandler PropertyChanged;
- public string Senderuser
- {
- get
- {
- return senduser;
- }
- set
- {
- senduser = value;
- if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
- {
- this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
- }
- }
- }
- public string Topic
- {
- get
- {
- return topic;
- }
- set
- {
- topic = value;
- if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
- {
- this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
- }
- }
- }
- public string Ffile
- {
- get
- {
- return file;
- }
- set
- {
- file = value;
- if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
- {
- this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
- }
- }
- }
- public string Time
- {
- get
- {
- return time;
- }
- set
- {
- time = value;
- if (this.PropertyChanged != null)//激發事件,參數爲Age屬性
- {
- this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
- }
- }
- }
- public MailList() { }
- public MailList(string senduser,string topic,string file,string time)
- {
- this.senduser = senduser;
- this.topic = topic;
- this.file = file;
- this.time = time;
- }
- }
- }
現在我們可以看到我們剛纔綁定的屬性就在這個類中,那麼該怎樣應用呢
下面來看一下我的應用代碼
- private List<MailList> maillist;
- maillist = new List<MailList>();
以上的代碼是聲明一個list來保存我們插入的數據的,由於我的源代碼是從服務器中得到的郵件列表。
- maillist.Add(new MailList("xxxxxx", "xxxxxxxx", "xxxxxx", "xxxxxx"));
- receiveList.ItemsSource = maillist;
如果這樣寫那麼那麼上面的途中得到的就是xxxxxx了。
那麼綁定就是這樣了。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
本文標題: C#中WPF ListView綁定數據的實例詳解
本文地址: http://www.cppcns.com/ruanjian/csharp/205903.html