WPF& amp; LINQ

时间:2013-01-10 11:40:55

标签: c# wpf linq gridview binding

情景:
我需要从代码中构建一个gridview,它将为每个列设置绑定集,而我的数据包含每天制造的每个产品的数量(在特定日期之间,但我得到了覆盖)。

问题:
我知道如何通过XAML绑定具有特定属性的类但这个东西是动态的,因为我不知道在这些特定日期生产了什么产品,并且不能保证所有产品都是。所以我需要构建一个动态(想想ObservableCollection)选项,它允许我在ObservableCollection中添加绑定到我的简单类的项(或者你认为最适合这个的任何东西)。

当前代码:
这是我用来获得产品和数量的简单类,应该是每个日期:

        public class MonthlyProducts
{
    public string ProductID { get; set; }
    public int Amount { get; set; }
}

现在我知道这些产品确实是在制造的:

    var ProductsInDates =
                    from Production in context.production
                    where Production.ProductionDate >= dtpStartDate.SelectedDate && Production.ProductionDate <= dtpEndDate.SelectedDate
                    group Production by Production.ProductID into Products
                    select new
                    {
                        ID = Products.Key
                    };

这就是我构建表的方法,这里我不知道如何通过代码绑定数据:

    foreach (var item in ProductsInDates)
            {
                MonthlyProducts temp = new MonthlyProducts();
                temp.ProductID = item.ID;
                temp.Amount = 0;
                MonthObservable.Add(temp);
                GridViewColumn TempColumn = new GridViewColumn();
                TempColumn.DisplayMemberBinding = new Binding(temp.ProductID);
                TempColumn.Header = temp.ProductID;
                TempColumn.Width = 100;
                MonthlyGridView.Columns.Add(TempColumn);
            }

现在是困难的部分,这是我获得制造的所有产品的数量(或者说我还没有获得它的shell因为我不知道该怎么做):

    var MonthProduction =
                    from Production in context.production
                    where Production.ProductionDate >= dtpStartDate.SelectedDate && Production.ProductionDate <= dtpEndDate.SelectedDate
                    group Production by Production.ProductionDate into MonthlyReport
                    select new
                    {
                        DateID = MonthlyReport.Key,
                        Row = MonthlyReport
                    };

这只是我试图玩这个。

基本上我需要知道我在数据列上面做的Binding是否会起作用,因为我还不能检查它,我需要知道如何在最后一次LINQ查询中为每个日期求和每个产品。

我希望我没有忘记任何事情,如果是这样,请告诉我,我会提供所需的所有信息。

0 个答案:

没有答案