我正在尝试将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行(至少它们在悬停时会改变颜色),但没有数据或任何类型的文本。我哪里出错?
答案 0 :(得分:2)
首先,我不相信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
。