I've already checked these links but didn't solve my problem.
I have two model classes(Order.cs and OrderItems.cs) and a View(Invoice.xaml)
There is ObservableCollection(OrderItem) in Order class and a datagrid in the Invoice.xaml. I need to bind ObservableCollection to the datagrid. Problem is, binding by writing xaml codes does not automatically update the datagrid when items are added to the ObservableCollection.
Codes are shown below
Order Class
public class Order
{
public Order()
{
OrderItems =new ObservableCollection<OrderItem>();
}
public ObservableCollection<OrderItem> OrderItems { get; set; }
public void GetOrderDetails(string customerId)
{
// method for getting set of OrderItmes objects and add to the
// ObservableCollection<OrderItem>
}
}
OrderItem Class
public class OrderItem
{
public OrderItem(string supplierId, string itemId, string itemName,decimal weight)
{
// some codes here
}
public string SupplierId { get; set; } // Supplier's ID
public string ItemId { get; set; } // Item ID
public string ItemName { get; set; }// Item Name
public decimal Weight { get; set; } // weight of the item
}
xaml code of the Invoice.xaml (for the simplicity only the necessary codes are shown. Model is the package of the Order and OrderItem classes.
xmlns:model="clr-namespace:Onion.Model"
<Window.Resources>
<model:Order x:Key="Order"/>
</Window.Resources>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" CanUserResizeRows="False" Grid.ColumnSpan="8"
Margin="0,0,30.5,0" CanUserResizeColumns="False" CanUserReorderColumns="False"
CanUserSortColumns="False" CanUserAddRows="False" IsReadOnly="True"
DataContext="{Binding Source={StaticResource Order}}" ItemsSource="{Binding OrderItems}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding SupplierId}" CanUserResize="False" FontSize="16"
Header="Supplier" Width="0.18*" />
<DataGridTextColumn Binding="{Binding ItemId}" CanUserResize="False" FontSize="16"
Header="ItemID" Width="0.13*" />
<DataGridTextColumn Binding="{Binding ItemName}" CanUserResize="False" FontSize="16"
Header="Item name" Width="0.2*" />
<DataGridTextColumn Binding="{Binding Weight}" CanUserResize="False" FontSize="16"
Header="Weight" Width="0.1*" />
</DataGrid>
But I can achieve this without writing the xaml codes by writting the code in the Invoice.xaml.cs class where it is necessary.
private void txtBox_PreviewKeyDown(object sender, KeyEventArgs e){
Order _order = new Order();
_order.GetOrderDetails(customerId);// add OrderItems to the ObservableCollection
// If this code is written some xaml codes are not needed (they are shown below in the question
dataGrid.ItemsSource = _order.OrderItems; // OrderItems is the ObservableCollection<OrderItem>
}
(including Binding each column to a property in OrderItem) Following codes are not necessary if the line dataGrid.ItemsSource = _order.OrderItems; is used.
xmlns:model="clr-namespace:Onion.Model"
<Window.Resources>
<model:Order x:Key="Order"/>
</Window.Resources>
DataContext="{Binding Source={StaticResource Order}}" ItemsSource="{Binding OrderItems}"
Can anyone point me how to overcome this problem.