title: How to properly bind a list to a WPF DataGrid
body: <p>I'm trying to bind a list to a datagrid using ItemSource:</p>
  
  <p>xaml.cs</p>
  
  <pre><code>&lt;UserControl x:Class="DDCUI.CommDiagnosisWPFCtrl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" Height="800" Width="300"&gt;
      &lt;DockPanel&gt;
          &lt;DataGrid DockPanel.Dock="Top" Height="300" AutoGenerateColumns="True" Name="DGComm" CanUserResizeColumns="True" IsReadOnly="True" ItemsSource="{Binding Source=dataGridRows}"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="No." Binding="{Binding Number}" Width="0.1*"/&gt;
                  &lt;DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="0.1*" /&gt;
                  &lt;DataGridTextColumn Header="Protocol" Binding="{Binding Protocol}" Width="0.15*" /&gt;
                  &lt;DataGridTextColumn Header="Source" Binding="{Binding Source}" Width="0.15*" /&gt;
                  &lt;DataGridTextColumn Header="Destination" Binding="{Binding Destination}" Width="0.15*" /&gt;
                  &lt;DataGridTextColumn Header="Data" Binding="{Binding Data}" Width="0.5*" /&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
          &lt;RichTextBox DockPanel.Dock="Bottom" Height="150" Name="RtbHexCode"/&gt;
          &lt;TreeView DockPanel.Dock="Bottom" Height="200" Name="TreeViewDecode"/&gt;
  
      &lt;/DockPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>public class CommDGDataSource
  {
      public string Number { get; set; }
      public string Time { get; set; }
      public string Protocol { get; set; }
      public string Source { get; set; }
      public string Destination { get; set; }
      public string Data { get; set; }
  }
  
  private List&lt;object&gt; dataGridRows = new List&lt;object&gt;();
  
  private void DGAddRow(string name, FunctionType ft)
  {
      CommDGDataSource ds = new CommDGDataSource();
      ds.Protocol = name;
      ds.Source = "";
      ds.Destination = "";
      ds.Number = rowCount.ToString();
      ds.Data = "";
      ds.Time = "";
      dataGridRows.Add(ds);
  }
  </code></pre>
  
  <p>When DGAddRow is called, the DataGrid doesn't update its values.</p>
  
  <p>What am I doing wrong here? Any help would be appreciated.</p>
  
  <p>Also, the XAML pre-generates empty rows even before they are filled with data. I want to be able to display only the rows that are filled with items (and add the new rows to the bottom of the last filled row). How can I accomplish this?</p>