[英]Binding ObservableCollection to listView - rows are there, but no content
我试图将Observablecollection绑定到WPF项目中的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("email1@gmail.com", "copy 1", "Subject 1", "BodyText 1", "john.doe@gmail.com", "27.11.2013"));
List.Add(new Message("email2@gmail.com", "copy 2", "Subject 2", "BodyText 2", "john.doe@gmail.com", "27.11.2013"));
List.Add(new Message("email3@gmail.com", "copy 3", "Subject 3", "BodyText 3", "john.doe@gmail.com", "27.11.2013"));
List.Add(new Message("email4@gmail.com", "copy 4", "Subject 4", "BodyText 4", "john.doe@gmail.com", "27.11.2013"));
List.Add(new Message("email5@gmail.com", "copy 5", "Subject 5", "BodyText 5", "john.doe@gmail.com", "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行(至少它们在悬停时会改变颜色),但没有数据或任何类型的文本。 我哪里做错了?
首先,我不相信Path
是必要的,因为您已经定义了ItemsSource
。
其次,你的构造函数应该是:
this.receiver = receiver;
this.copy = copy;
this.subject = subject;
this.bodyText = bodyText;
this.sender = sender;
this.date = date;
你有this.
在等号的错误一侧。
最后,确保您的属性命名正确,我在Message类中看到Sender
命名错误。
public string Posiljatelj // <- that should be named Sender
{
get { return this.sender; }
set { this.sender = value; }
}
您的xaml绑定到Sender
列中有一列,但您的Message类中没有Sender
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.