title: How to bind datagrid with two itemsSource in WPF?
body: <p>I have something like this in my xaml</p>
  
  <pre><code>&lt;DataGrid  Name="dgDisplay" &gt;
      &lt;DataGrid.Columns&gt;
            &lt;DataGridTextColumn IsReadOnly="True" x:Name="dgItemCode" Width="100" Header="Barcode" Binding="{Binding ItemCode}" /&gt;
             &lt;DataGridTextColumn IsReadOnly="True" x:Name="dgItemName" Width="200" Header="Item Name" Binding="{Binding ItemName}" /&gt;
             &lt;DataGridTextColumn IsReadOnly="True" x:Name="dgItemPrice" Width="100" Header="Item Price" Binding="{Binding ItemPrice, StringFormat=RM {0}}" /&gt;
             &lt;DataGridTextColumn IsReadOnly="True" x:Name="dgQuantity" Width="150" Header="Quantity" Binding="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}"   /&gt;
          &lt;/DataGrid.Columns&gt;
  
        &lt;DataGrid.RowDetailsTemplate &gt;
           &lt;DataTemplate  x:Name="dtItemDisplay"  &gt;
             &lt;StackPanel  Name="spItemDisplay2" HorizontalAlignment="Stretch" Background="White" &gt;
                &lt;Grid&gt;
                  &lt;TextBlock Text="Discount: " FontWeight="Bold" Grid.Column="2" Grid.Row="1"/&gt;
                   &lt;TextBox KeyDown="TxtDisc_KeyDown"  x:Name="txtDisc" Text="{Binding Discount, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Grid.Row="1"/&gt;
  
                    &lt;DataGrid ItemsSource="{Binding itemSubDisplayList2}" x:Name="dgItemSub"&gt;
  
                                          &lt;DataGrid.Columns&gt;
                                              &lt;DataGridTextColumn Header="item sub" Binding="{Binding ItemIdSub}"/&gt;
                                              &lt;DataGridTextColumn Header="item sub code" Binding="{Binding ItemSubCode}"/&gt;
                                          &lt;/DataGrid.Columns&gt;
                                      &lt;/DataGrid&gt;
               &lt;/Grid&gt;
              &lt;/StackPanel&gt;
             &lt;/DataTemplate&gt;
            &lt;/DataGrid.RowDetailsTemplate&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>I have two list or sources that I need to bind in this data grid. for item in <code>datagrid.column</code> and textbox <code>Discount</code> (in RowDetailsTemplate ) its using <code>itemsSource = itemDisplayList</code> meanwhile for item inside 'dgItemSub' its using itemsSource <code>itemSubDisplayList2</code></p>
  
  <p>the thing is I've this code at my code behind </p>
  
  <pre><code> dgDisplay.ItemsSource = itemDisplayList;
   dgDisplay.ItemsSource = itemSubDisplayList2;
  </code></pre>
  
  <p>but it seem like I can't used both. only item that bind to <code>itemSubDisplayList2</code> works. but if I do not do this it doesn't show any output inside my datagrid.</p>
  
  <p>I've try something like this</p>
  
  <pre><code>  &lt;DataGrid Name="dgDisplay" ItemsSource="{Binding itemDisplayList}"&gt;
  
  &lt;DataGrid ItemsSource="{Binding itemSubDisplayList2}" x:Name="dgItemSub"&gt; 
  </code></pre>
  
  <p>without doing <code>dgDisplay.ItemsSource = itemDisplayList;</code> and <code>dgDisplay.ItemsSource = itemSubDisplayList2;</code> at code behind but it didn't work. </p>
  
  <p>How can I bind from 2 itemsSource in this ?</p>
  
  <p>or is there a way i can access <code>dgItemSub</code> from code behind. like I want to something like this</p>
  
  <pre><code>dgItemSub.ItemsSource = itemSubDisplayList2;
  </code></pre>
  
  <p>this is how i code the <code>itemDisplayList</code> and <code>itemSubDisplayList2</code> and I code this at code behind</p>
  
  <pre><code>       ItemDisplay itemDisplay = new ItemDisplay()
                      {
                          ItemCode = item.ItemCode,
                          ItemName = item.ItemName,
                          ItemPrice = item.ItemPrice,
                          Quantity = 1,
                      };
  
                      itemDisplayList.Add(itemDisplay);
  
  
                      foreach (var id in itemIds)
                      {
                          SimpleItem item2 = simpleItemBO.GetItemByItemId(id);
                          itemList.Add(item2);
  
                          ItemSubDisplay itemSubDisplay = new ItemSubDisplay();
                          itemSubDisplay.ItemIdSub = item2.ItemId;
                        itemSubDisplay.ItemSubCode = item2.ItemCode;
                          itemSubDisplayList.Add(itemSubDisplay);
                      }
  
                      itemSubDisplayList2.AddRange(itemSubDisplayList);
  </code></pre>