【发布时间】:2012-04-02 22:16:01
【问题描述】:
我希望我的列表视图的数据上下文绑定到一个可观察的集合。现在我有:
// CurrentEmploye = some employee
Entities.DatabaseModel m = new Entities.DatabaseModel();
var q = from t in m.TimeSheet
join emp in m.Employees on t.idEmployee equals emp.id
where emp.id == CurrentEmploye.id
select new
{
firstName = emp.firstName,
lastName = emp.lastName,
position = emp.position,
clockInDate = t.clockInDate,
clockOutDate = t.clockOutDate,
};
listView1.DataContext = q;
该代码正确填充列表视图。现在,每当我更新列表视图项目时,我都会更新列表视图。
我希望变量q 为 ObservableCollection 类型,而不必创建包含 firstName、lastName、position 等的自定义类...我该怎么做?
【问题讨论】:
-
我假设您尝试了
listView1.DataContext = new ObservableCollection(q),但它不起作用,对吧?... -
新的 ObservableCollection(q);需要一个类型,我不知道类型。也许我会通过反思得到它。谢谢,这是一个有用的评论。
-
啊,你说得对,我完全忘记了对方法有效的东西并不总是对构造函数有效。让我们等 Jon Skeet 吧:)
-
取消等待! He did it already!
-
我想我当时有一个重复的问题。把它作为答案,我会接受它。感谢您的帮助。
标签: c# casting observablecollection iqueryable