国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中WPF ListView綁定數(shù)據(jù)的實例詳解

2019-10-29 21:08:14
字體:
來源:轉載
供稿:網(wǎng)友

C#中WPF ListView綁定數(shù)據(jù)的實例詳解

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

C#,ListView,綁定數(shù)據(jù),WPF

那么怎樣實現(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="時間" 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)//激發(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)在我們可以看到我們剛才綁定的屬性就在這個類中,那么該怎樣應用呢

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

private List<MailList> maillist; 
maillist = new List<MailList>(); 

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

maillist.Add(new MailList("xxxxxx", "xxxxxxxx", "xxxxxx", "xxxxxx"));  
receiveList.ItemsSource = maillist; 

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

那么綁定就是這樣了。

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


注:相關教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌乐县| 泽州县| 赣榆县| 仁寿县| 宝清县| 玉林市| 清涧县| 班玛县| 龙口市| 湘潭市| 长岛县| 潍坊市| 利津县| 本溪| 芜湖市| 枣庄市| 揭阳市| 兴化市| 邵武市| 辉南县| 磐石市| 富民县| 纳雍县| 新化县| 斗六市| 彭山县| 泰州市| 新和县| 镇宁| 九江市| 鸡西市| 齐河县| 张家口市| 美姑县| 保靖县| 调兵山市| 阳新县| 汉中市| 威远县| 乌兰浩特市| 措勤县|