溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中WPF ListView綁定數(shù)據(jù)的示例分析

發(fā)布時(shí)間:2021-07-15 13:59:51 來(lái)源:億速云 閱讀:499 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹了C#中WPF ListView綁定數(shù)據(jù)的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

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

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

C#中WPF ListView綁定數(shù)據(jù)的示例分析

那么怎樣實(shí)現(xiàn)這樣的效果的呢,這就要用綁定了。

我們先來(lái)看一看他的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=作為綁定源的類中的成員屬性}

下面來(lái)看一下綁定的類

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)用呢

下面來(lái)看一下我的應(yīng)用代碼

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

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

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

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

那么綁定就是這樣了。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#中WPF ListView綁定數(shù)據(jù)的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI