WPF Datagrid 绑定到 ObservableCollection 未生成列

发布于 2024-09-16 06:36:19 字数 803 浏览 4 评论 0原文

我将数据网格绑定到一些数据并使用 AutoColumnGeneration。当对数据集使用常规 linq 查询时,一切正常:

var queryAll = from actor in actorsAll
select new
   {
       ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
   };

    MalatDetailsBudgetGridUC.ItemsSource = queryAll;

但由于我希望我的网格绑定到 ObservableCollection,所以我尝试使用以下内容:

ActorsCollection collection = new ActorsCollection(actorType);
var queryAll = from actor in collection
    select new
    {
        ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
    };

MalatDetailsBudgetGridUC.ItemsSource = queryAll;

当使用它时,我的网格准确地填充了(微小的薄)行应该如此,但不会生成任何列。

顺便说一句 - ActorsCollection 是一个已实现的 ObservableCollection,它将自身与 Actor 实体相加。

请帮助!

I am binding a datagrid to some data and using the AutoColumnGeneration. When using a regular linq query against a dataset, everything works fine :

var queryAll = from actor in actorsAll
select new
   {
       ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
   };

    MalatDetailsBudgetGridUC.ItemsSource = queryAll;

But as i want my grid to be binded to an ObservableCollection, i am trying to use the followings:

ActorsCollection collection = new ActorsCollection(actorType);
var queryAll = from actor in collection
    select new
    {
        ActorName = actor.IsPerson ? actor.FirstName + " " + actor.LastName : actor.CompanyName
    };

MalatDetailsBudgetGridUC.ItemsSource = queryAll;

When using this, my grid get populated with (tiny thin) rows exactly as it should be, but no columns are generated.

B.T.W - ActorsCollection is an implemented ObservableCollection that adds itself with the Actor entities.

Please Help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若水般的淡然安静女子 2024-09-23 06:36:19

好吧,不能说我理解太多:标题说数据网格,但在问题中你谈论树视图。

假设您将 queryAll 设置为网格的 DataContext,两个代码块之间应该没有任何区别。您确定 DataContext 设置/绑定正确吗?

编辑:还尝试使用调试器查看匿名构造函数创建的类型,这可能会给您一些想法。

编辑:要在此处添加答案,问题是第二种形式创建了一个迭代器,并且数据网格(有趣的是)被它弄糊涂了。最快的方法是强制枚举器使用 ToList() 或类似方法生成集合。

Well, can't say I understand much: the title says datagrid, but in the question you talk about treeview.

There shouldn't be any difference between the two code blocks, assuming you set the queryAll as the DataContext of the grid. Are you sure the DataContext is set/bound correctly?

edit: also try looking with the debugger at the types created by the anonymous constructors, that might give you some ideas.

edit: to add the answer here, the problem was that the second form creates an iterator and the datagrid (interestingly) gets confused by it. The quickest way is to force the enumerator to generate the collection with ToList() or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文