0

场景:
我需要从代码中构建一个网格视图,该代码将为每一列设置绑定,而我的数据包含每天制造的每种产品的数量(在特定日期之间,但我已经涵盖了)。

问题:
我知道如何通过 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);
            }

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

    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
                    };

这只是我试图玩这个。

基本上我需要知道我上面对数据列所做的绑定是否有效,因为我还不能检查它,我需要知道如何在最后一个 LINQ 查询中对每个日期的每个产品求和。

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

4

0 回答 0