如何将WPF数据网格绑定到包含的ObservableCollection

时间:2017-09-21 14:48:04

标签: c# wpf

我是C#和WPF的新手,所以任何帮助都会非常感激。

更新 我有两个对象,PurchaseOrder(PO)和PurchaseOrderDetails(POD)。 PO包含一个POD的ObservableCollection。

我能够从我的数据库中获取所需的所有数据。 因此,当OfficeAdmin窗口加载时,我从数据库表中获取所有采购订单并将其显示在PO数据网格中,当在dgPurchaseOrder中进行选择时,当前采购订单的详细信息将显示在dgPODescription中。

但是我想在dgPurchaseOrders中显示POD的'PODate'属性,我看了一下评论中提供的@Default链接,但我仍然没有让绑定工作,我错过了什么?

 public class PurchaseOrder : INotifyPropertyChanged
{
    int _id;
    string _customerName;
    int _customerId;

    int _purchaseOrderNumber;  
    int _purchaseOrderDetailsId;


    private ObservableCollection<Customer> _customer;
    private ObservableCollection<PurchaseOrderDetails> _purchaseOrderDetails;
    public ObservableCollection<Customer> Customer
    {
        get { return _customer; }
        set
        {
            _customer = value;
            OnPropertyChanged("Customer");
        }
    }
    public PurchaseOrder()
    {
        _customerName = String.Empty;
    }
    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            OnPropertyChanged("Id");
        }
    }

    public string CustomerName
    {
        get { return _customerName; }
        set
        {
            _customerName = value;
            OnPropertyChanged("CustomerName");
        }
    }
    public int CustomerId
    {
        get { return _customerId; }
        set
        {
            _customerId = value;
            OnPropertyChanged("CustomerId");
        }
    }
    public int PurchaseOrderId
    {
        get { return _purchaseOrderDetailsId; }
        set
        {
            _purchaseOrderDetailsId = value;
            OnPropertyChanged("PurchaseOrderDetailsId");
        }
    }
    public int PurchaseOrderNumber
    {
        get { return _purchaseOrderNumber; }
        set
        {
            _purchaseOrderNumber = value;
            OnPropertyChanged("PurchaseOrderDetailsId");
        }
    }
    public ObservableCollection<PurchaseOrderDetails> PurchaseOrderDetails
    {
        get { return _purchaseOrderDetails; }
        set
        {
            _purchaseOrderDetails = value;
            OnPropertyChanged("PurchaseOrderDetails");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}
 public class PurchaseOrderDetails : INotifyPropertyChanged
{
    int _id;
    int _orderNumber;
    string _customerName;
    int _costPrice;
    int _quantity;
    int _totalAmount;
    int _vat;
    string _date;
    string _deliveryDate;
    int _productId;
    string _productName;

    public PurchaseOrderDetails()
    {
        _customerName = string.Empty;
        _productName = string.Empty;


    }
    public int Id
    {
        get { return _id; }
        set
        {
            _id = value;
            OnPropertyChanged("Id");
        }
    }
    public int OrderNumber
    {
        get { return _orderNumber; }
        set
        {
            _orderNumber = value;
            OnPropertyChanged("Quantity");
        }
    }
    public string CustomerName
    {
        get { return _customerName; }
        set
        {
            _customerName = value;
            OnPropertyChanged("CustomerName");
        }
    }
    public string PODate
    {
        get { return _date; }
        set
        {
            _date = value;
            OnPropertyChanged("PODate");
        }
    }
    public string DeliveryDate
    {
        get { return _deliveryDate; }
        set
        {
            _deliveryDate = value;
            OnPropertyChanged("DeliveryDate");
        }
    }
    public int ProductId
    {
        get { return  _productId;  }
        set
        {
            _productId = value;
            OnPropertyChanged("ProductId");
        }
    }

    public string ProductName
    {
        get { return _productName; }
        set
        {
            _productName = value;
            OnPropertyChanged("ProductName");
        }
    }
    public int CostPrice
    {
        get { return _costPrice; }
        set
        {
            _costPrice = value;
            OnPropertyChanged("CostPrice");
        }
    }
    public int Quantity
    {
        get { return _quantity; }
        set
        {
            _quantity = value;
            OnPropertyChanged("Quantity");
        }
    }
    public int Vat
    {
        get { return _vat; }
        set
        {
            _vat = value;
            OnPropertyChanged("Vat");
        }
    }
    public int TotalAmount
    {
        get { return _totalAmount; }
        set
        {
            _totalAmount = value;
            OnPropertyChanged("TotalAmount");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property));
    } 
}

OfficeAdmin.xaml

<DataGrid x:Name="dgPurchaseOrder"                             
                              Style="{StaticResource AdminGridStyle}" 
                                   SelectionChanged="dgPurchaseOrder_SelectionChanged"
                                  MouseUp="dgPurchaseOrder_MouseUp">

                            <DataGrid.Columns>
                                <DataGridTextColumn x:Name="dgPOCustomerName"
                                                Header="Customer Name" 
                                                Binding="{Binding CustomerName}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="PoPurchaseOrderNumber"
                                                Header="Order Number" 
                                                Binding="{Binding PurchaseOrderNumber}" 
                                                Width="1*" />


                                <DataGridTemplateColumn Header="Date">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding PurchaseOrderDetails.PODate}"/>

                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>

                        </DataGrid>
 <DataGrid x:Name="dgPODescription" 
                                  Style="{StaticResource AdminGridStyle}" 
                                  ItemsSource="{Binding PurchaseOrderDetails}">

                            <DataGrid.Columns>
                                <DataGridTextColumn x:Name="PoProduct"
                                                Header="Product"                                                     
                                                Binding="{Binding ProductName}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="PoQuantity"
                                                Header="Quantity" 
                                                Binding="{Binding Quantity}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="PoCostPrice"
                                                Header="Cost Price" 
                                                Binding="{Binding CostPrice}" 
                                                Width="1*" />


                                <DataGridTextColumn x:Name="PoTotalAmount" 
                                                Header="Total Amount" 
                                                Binding="{Binding TotalAmount}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="PoVat"
                                                Header="Vat" 
                                                Binding="{Binding Vat}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="PoDate"
                                                Header="Date" 
                                                Binding="{Binding PODate}" 
                                                Width="1*" />

                                <DataGridTextColumn x:Name="DeliveryDate"
                                                Header="Delivery Date" 
                                                Binding="{Binding DeliveryDate}" 
                                                Width="1*" />


                            </DataGrid.Columns>
                        </DataGrid>

OfficeAdmin.xaml.cs

private void Window_Loaded(object sender, RoutedEventArgs e)
    {  
        dgPurchaseOrder.ItemsSource = vm.getAllPurchasOrders();
        dgPurchaseOrder.SelectedIndex = 0;
        dgPODescription.ItemsSource = vm.PurchaseOrderDetails; 
    } 
private void dgPurchaseOrder_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var purchaseOrder = dgPurchaseOrder.SelectedItem as PurchaseOrder;
        if(purchaseOrder!=null)vm.setCurrentPurchaseOrder(purchaseOrder);
        dgPODescription.ItemsSource = vm.PurchaseOrderDetails;
    }

vmOfficeAdmin.cs

public ObservableCollection<PurchaseOrder> PurchaseOrder { get; set; }
    public ObservableCollection<Customer> PurchaseOrderCustomer { get; set;}   
    public ObservableCollection<PurchaseOrderDetails> PurchaseOrderDetails {get; set; }
    public PurchaseOrder CurrentPurchaseOrder { get; set; }
public vmUserManagement()
    {        
        CurrentPurchaseOrder = new PurchaseOrder();
        PurchaseOrder = new ObservableCollection<PurchaseOrder>();
        PurchaseOrderCustomer = new ObservableCollection<Customer>();
        PurchaseOrderDetails = new ObservableCollection<PurchaseOrderDetails>();           
    }                                                                  


public ObservableCollection<PurchaseOrder> getAllPurchasOrders()
    {
       PurchaseOrder = RepoDapper.getAllPurchaseOrders();           
       return PurchaseOrder;
    }
public void setCurrentPurchaseOrder(PurchaseOrder purchaseOrder)
    {            
        CurrentPurchaseOrder = purchaseOrder;
        PurchaseOrderDetails.Clear();
        CurrentPurchaseOrder.PurchaseOrderDetails = RepoDapper.getPurchaseOrderDetails(CurrentPurchaseOrder.Id);
        CurrentPurchaseOrder.Customer = RepoDapper.getPurchaseOrderCustomerDetails(CurrentPurchaseOrder.CustomerId); 


        foreach (var b in CurrentPurchaseOrder.PurchaseOrderDetails)
        {
            PurchaseOrderDetails.Add(b);
        }

        foreach(var d in CurrentPurchaseOrder.Customer)
        {
            PurchaseOrderCustomer.Add(d);
        }
    }

0 个答案:

没有答案