C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
C#中WPF ListView綁定數(shù)據(jù)的實(shí)例詳解
WPF中ListView用來顯示數(shù)據(jù)十分方便, 我們可以將它分成幾個(gè)列,每一個(gè)列用來顯示一條數(shù)據(jù),但是又是在一方之中。

那么怎樣實(shí)現(xiàn)這樣的效果的呢,這就要用綁定了。
我們先來看一看他的xmal代碼
<ListView Name="receiveList" Grid.Row="0">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="發(fā)件人"
Width="200"
DisplayMemberBinding="{Binding Path=Senderuser}" />
<GridViewColumn Header="主題"
Width="350"
DisplayMemberBinding="{Binding Path=Topic}" />
<GridViewColumn Header="附件" DisplayMemberBinding="{Binding Path=Ffile}"
Width="200" />
<GridViewColumn Header="時(shí)間" Width="150" DisplayMemberBinding="{Binding Path=Time}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
上面的代碼中每一個(gè)GridViewColumn都有一個(gè)綁定{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)//激發(fā)事件,參數(shù)為Age屬性
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}
public string Topic
{
get
{
return topic;
}
set
{
topic = value;
if (this.PropertyChanged != null)//激發(fā)事件,參數(shù)為Age屬性
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}
public string Ffile
{
get
{
return file;
}
set
{
file = value;
if (this.PropertyChanged != null)//激發(fā)事件,參數(shù)為Age屬性
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}
public string Time
{
get
{
return time;
}
set
{
time = value;
if (this.PropertyChanged != null)//激發(fā)事件,參數(shù)為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;
}
}
}
現(xiàn)在我們可以看到我們剛才綁定的屬性就在這個(gè)類中,那么該怎樣應(yīng)用呢
下面來看一下我的應(yīng)用代碼
private List<MailList> maillist;
maillist = new List<MailList>();
以上的代碼是聲明一個(gè)list來保存我們插入的數(shù)據(jù)的,由于我的源代碼是從服務(wù)器中得到的郵件列表。
maillist.Add(new MailList("xxxxxx", "xxxxxxxx", "xxxxxx", "xxxxxx"));
receiveList.ItemsSource = maillist;
如果這樣寫那么那么上面的途中得到的就是xxxxxx了。
那么綁定就是這樣了。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- c# winform treelistview的使用(treegridview)實(shí)例詳解
- C# WPF ListView控件的實(shí)例詳解
- C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
- C#中ListView控件實(shí)現(xiàn)窗體代碼
- C#下listview如何插入圖片
- C#實(shí)現(xiàn)listview Group收縮擴(kuò)展的方法
- C#實(shí)現(xiàn)帶進(jìn)度條的ListView
- C#實(shí)現(xiàn)讀取DataSet數(shù)據(jù)并顯示在ListView控件中的方法
- 一文掌握C# ListView控件的用法和示例代碼
相關(guān)文章
C#借助OpenCvSharp讀取攝像頭并顯示的實(shí)現(xiàn)示例
OpenCvSharp是一個(gè)OpenCV的.Net wrapper,應(yīng)用最新的OpenCV庫(kù)開發(fā),本文主要介紹了C#借助OpenCvSharp讀取攝像頭并顯示的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
C# 如何在WINForm程序中創(chuàng)建XML文件
這篇文章主要介紹了C# 如何在WINForm程序中創(chuàng)建XML文件,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫的16進(jìn)制Unicode的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫的16進(jìn)制Unicode的方法,分析了轉(zhuǎn)換的技巧并以實(shí)例形式給出了具體的轉(zhuǎn)換方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法)
本文主要介紹了C# Bitmap圖像處理(含增強(qiáng)對(duì)比度的三種方法),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

