How to bind ObservableCollection with datagrid WPF
<p>I've already checked these links but didn't solve my problem.</p>

<ul>
<li><p><a href="https://stackoverflow.com/questions/24539022/how-to-bind-wpf-datagrid-to-observablecollection">How to bind WPF DataGrid to ObservableCollection</a></p></li>
<li><p><a href="https://stackoverflow.com/questions/15087131/bind-an-observablecollection-to-a-wpf-datagrid-gris-stay-empty">Bind an ObservableCollection to a wpf datagrid : Grid stays empty</a></p></li>
</ul>

<p>I have two model classes(<strong>Order.cs</strong> and <strong>OrderItems.cs</strong>) and a View(<strong>Invoice.xaml</strong>) </p>

<p>There is <strong>ObservableCollection(OrderItem)</strong> 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. </p>

<p>Codes are shown below</p>

<p>Order Class</p>

<pre><code>public class Order
{
    public Order()
    {
        OrderItems =new ObservableCollection&lt;OrderItem&gt;();
    }

    public ObservableCollection&lt;OrderItem&gt; OrderItems { get; set; }

    public void GetOrderDetails(string customerId)
    {
        // method for getting set of OrderItmes objects and add to the 
        // ObservableCollection&lt;OrderItem&gt;
    }
}
</code></pre>

<p>OrderItem Class</p>

<pre><code>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
}
</code></pre>

<p>xaml code of the Invoice.xaml (for the simplicity only the necessary codes are shown. <strong>Model</strong> is the package of the Order and OrderItem classes.</p>

<pre><code>    xmlns:model="clr-namespace:Onion.Model"
&lt;Window.Resources&gt;
    &lt;model:Order x:Key="Order"/&gt;
&lt;/Window.Resources&gt;

&lt;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}"&gt;
            &lt;DataGrid.Columns&gt;
                &lt;DataGridTextColumn Binding="{Binding SupplierId}" CanUserResize="False" FontSize="16"
                    Header="Supplier" Width="0.18*" /&gt;
                &lt;DataGridTextColumn Binding="{Binding ItemId}" CanUserResize="False" FontSize="16"
                    Header="ItemID" Width="0.13*" /&gt;
                &lt;DataGridTextColumn Binding="{Binding ItemName}" CanUserResize="False" FontSize="16"
                    Header="Item name" Width="0.2*" /&gt;
                &lt;DataGridTextColumn Binding="{Binding Weight}" CanUserResize="False" FontSize="16" 
                    Header="Weight" Width="0.1*" /&gt;

 &lt;/DataGrid&gt;
</code></pre>

<p>But I can achieve this without writing the xaml codes by writting the code in the Invoice.xaml.cs class where it is necessary.</p>

<pre><code>    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&lt;OrderItem&gt;  
}
</code></pre>

<p>(including Binding each column to a property in OrderItem) Following codes are not necessary if the line dataGrid.ItemsSource = _order.OrderItems; is used.</p>

<pre><code>xmlns:model="clr-namespace:Onion.Model"
&lt;Window.Resources&gt;
    &lt;model:Order x:Key="Order"/&gt;
 &lt;/Window.Resources&gt;
 DataContext="{Binding Source={StaticResource Order}}" ItemsSource="{Binding OrderItems}"
</code></pre>

<p>Can anyone point me how to overcome this problem.</p>