2013-11-27 42 views
0

我想在WPF項目中將Observablecollection綁定到listView。我創造了這樣的集合:將ObservableCollection綁定到listView - 行在那裏,但沒有內容

private ObservableCollection<Message> list = new ObservableCollection<Message>(); 

    public ObservableCollection<Message> List 
    { 
     get { return this.list; } 
     set { this.list = value; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     //Sample messages 
     List.Add(new Message("[email protected]", "copy 1", "Subject 1", "BodyText 1", "[email protected]", "27.11.2013")); 
     List.Add(new Message("[email protected]", "copy 2", "Subject 2", "BodyText 2", "[email protected]", "27.11.2013")); 
     List.Add(new Message("[email protected]", "copy 3", "Subject 3", "BodyText 3", "[email protected]", "27.11.2013")); 
     List.Add(new Message("[email protected]", "copy 4", "Subject 4", "BodyText 4", "[email protected]", "27.11.2013")); 
     List.Add(new Message("[email protected]", "copy 5", "Subject 5", "BodyText 5", "[email protected]", "27.11.2013")); 

     this.DataContext = this; 
    } 

類看起來是這樣的:

public class Message 
{ 
    private string receiver; 
    private string copy; 
    private string subject; 
    private string bodyText; 
    private string sender; 
    private string date; 

    public Message(string receiver, 
        string copy, 
        string subject, 
        string bodyText, 
        string sender, 
        string date) 
    { 
     receiver = this.receiver; 
     copy = this.copy; 
     subject = this.subject; 
     bodyText = this.bodyText; 
     sender = this.sender; 
     date = this.date; 
    } 

    public string Receiver 
    { 
     get { return this.receiver; } 
     set { this.receiver = value; } 
    } 
    public string Copy 
    { 
     get { return this.copy; } 
     set { this.copy = value; } 
    } 
    public string Subject 
    { 
     get { return this.subject; } 
     set { this.subject = value; } 
    } 
    public string BodyText 
    { 
     get { return this.bodyText; } 
     set { this.bodyText = value; } 
    } 
    public string Posiljatelj 
    { 
     get { return this.sender; } 
     set { this.sender = value; } 
    } 
    public string Date 
    { 
     get { return this.date; } 
     set { this.date = value; } 
    } 
} 

而XAML部分:

 <ListView x:Name="listLstView" ItemsSource="{Binding Path=List}" HorizontalAlignment="Left" Height="316" Margin="232,42,0,0" VerticalAlignment="Top" Width="734"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Subject" Width="250" DisplayMemberBinding="{Binding Path=Subject}"/> 
        <GridViewColumn Header="Sender" Width="250" DisplayMemberBinding="{Binding Path=Sender}"/> 
        <GridViewColumn Header="Date" Width="230" DisplayMemberBinding="{Binding Path=Date}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 

當我把我的鼠標指針的ListView區,5行存在(至少它們在懸停時改變顏色),但沒有數據或任何類型的文本。我哪裏做錯了?

+0

您似乎沒有在您的屬性上實現['INotifyPropertyChanged'接口](http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx)。你應該在你所有的屬性上實現這個接口,包括'List'屬性。 – Sheridan

回答

2

首先,我不相信Path是必要的,因爲您已經定義了您的ItemsSource

所有第二,你的構造應該是:

this.receiver = receiver; 
this.copy = copy; 
this.subject = subject; 
this.bodyText = bodyText; 
this.sender = sender; 
this.date = date; 

您在等號的錯誤的一邊有this.

最後,確保您的屬性命名正確,我看到Sender在您的Message類中被錯誤地命名。

public string Posiljatelj // <- that should be named Sender 
    { 
     get { return this.sender; } 
     set { this.sender = value; } 
    } 

你有你的一列在您的XAML綁定到Sender,但你卻不在你的郵件類Sender

+0

是的,我注意到,命名錯誤,它發生時,將代碼翻譯成英文。構造函數是致命的錯誤。它現在很好用。非常感謝。 – cvenko

相關問題