使用EntityFramework在DataGrid中绑定DataGrid

时间:2017-07-31 18:51:11

标签: c# wpf data-binding

Xaml:无法弄清楚如何绑定包含一个或多个记录的第二个DataGrid。

<DataGrid Name="lstPurchaseDataGrid" Visibility="Visible"  AutoGenerateColumns="False" Margin="20,0,20,0" IsReadOnly="True" materialDesign:DataGridAssist.CellPadding="4 2 2 2" materialDesign:DataGridAssist.ColumnHeaderPadding="4 2 2 2" ItemsSource="{Binding}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Purchase Id" Width="100" Binding="{Binding ID}"/>
                            <DataGridTextColumn Header="Vendor Name" Width="2*" Binding="{Binding Vendor.Name}"/>
                            <DataGridTextColumn Header="Parts Amount" Width="100" Binding="{Binding SubTotal}"/>
                            <DataGridTextColumn Header="Discount" Width="100" Binding="{Binding Discount}"/>
                            <DataGridTextColumn Header="Total Amount" Width="120" Binding="{Binding GrandTotal}"/>
                            <DataGridTextColumn Header="Purchase Date" Width="100" Binding="{Binding PurchaseDate, StringFormat=d}" />
                            <DataGridTextColumn Header="Created On" Width="100" Binding="{Binding CreatedOn}" />
                            <DataGridTextColumn Header="Status" Width="100" Binding="{Binding Status}" />
                        </DataGrid.Columns>
                        <DataGrid.RowDetailsTemplate>
                            <DataTemplate>
                                <DataGrid AutoGenerateColumns="False" x:Name="lstDetailsDataGrid" IsReadOnly="False" ItemsSource="{Binding PurchaseInfoes}">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Binding="{Binding Path=Id}" Header="Part Id" />
                                        <DataGridTextColumn Binding="{Binding Path=Part.PartNo}" Header="Part No" />
                                        <DataGridTextColumn Binding="{Binding Path=Part.Name}" Header="Name" />
                                        <DataGridTextColumn Binding="{Binding Path=Quantity}" Header="Quantity" />
                                        <DataGridTextColumn Binding="{Binding Path=CGST}" Header="CGST" />
                                        <DataGridTextColumn Binding="{Binding Path=SGST}" Header="SGST" />
                                        <DataGridTextColumn Binding="{Binding Path=Discount}" Header="Discount" />
                                        <DataGridTextColumn Binding="{Binding Path=Total}" Header="Total" />
                                    </DataGrid.Columns>
                                </DataGrid>
                            </DataTemplate>
                        </DataGrid.RowDetailsTemplate>
                    </DataGrid>

C#                 lstPurchaseDataGrid.Items.Clear();

            var purchaseList = (from purchases in dt.Purchases.Include("Vendor")
                                select purchases).ToList();
            lstPurchaseDataGrid.ItemsSource = purchaseList;

C#购买类代码

公共部分类购买     {         [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”,“CA2214:DoNotCallOverridableMethodsInConstructors”)]         公开购买()         {             this.PurchaseInfoes = new HashSet();             this.PurchasePayDetails = new HashSet();         }

    public int ID { get; set; }
    public int VendorID { get; set; }
    public decimal SubTotal { get; set; }
    public decimal Discount { get; set; }
    public decimal GrandTotal { get; set; }
    public string Status { get; set; }
    public System.DateTime PurchaseDate { get; set; }
    public string InvoiceNo { get; set; }
    public string Note { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> UpdatedOn { get; set; }
    public string UpdatedBy { get; set; }
    public Nullable<decimal> RemainingPayment { get; set; }

    public virtual Vendor Vendor { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PurchaseInfo> PurchaseInfoes { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<PurchasePayDetail> PurchasePayDetails { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我在第二个网格中添加了路径绑定,删除路径后它完美运行。

<强>之前: 绑定=&#34; {绑定路径= Id}&#34; 的 Binding =&#34; {Binding Id}