title: Add a Control to autogenerated ColumnHeader of DataGrid in WPF getting data from SQLite database
body: <p>I need to create an excel like filter for my data imported from SQLite database.</p>
  
  <p>Importing and showing data from database is doing well with</p>
  
  <pre><code>private void UpdateDataGrid(SQLiteConnection con, string sql)
  {
      DataSet dataSet = new DataSet();
      SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, con);
      dataAdapter.Fill(dataSet);
  
      dgMaterials.ItemsSource = dataSet.Tables[0].DefaultView;
  }
  </code></pre>
  
  <p>AutoGenerateColumns is true so that I get something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/E3NI2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E3NI2.png" alt="enter image description here"></a></p>
  
  <p>Data are shown in left side datagrid.
  In right side listbox I collect the generated column headers with</p>
  
  <pre><code>private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
  {
      lb.Items.Add(e.Column.Header.ToString());
  }
  </code></pre>
  
  <p>As I said, I want to create excel like filter buttons in header row beside every header text like this</p>
  
  <p><a href="https://i.stack.imgur.com/RVlWw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RVlWw.png" alt="enter image description here"></a></p>
  
  <p>On XAML site I started like this</p>
  
  <pre><code>&lt;DataGrid x:Name="dgMaterials"
            IsReadOnly="True"
            CanUserReorderColumns="False"
            CanUserResizeColumns="False"
            CanUserSortColumns="False"
            DockPanel.Dock="Left" AutoGeneratingColumn="dg_AutoGeneratingColumn"
            AlternatingRowBackground="LightGray"&gt;
  
      ... input a button next to every header text ...
  
  
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Autocreating of buttons in Headers is working with:</p>
  
  <pre><code>private void dg_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
  {
      ////string headertext = e.Column.Header.ToString();
  
      //StackPanel sp = new StackPanel();
      //TextBlock tb = new TextBlock();
      //tb.Text = e.Column.Header.ToString();
      //sp.Children.Add(tb);
  
      dgMaterials.AutoGeneratingColumn += (ss, ee) =&gt;
      {
          Button b = new Button() { Content = "...", Name = "btn_" + ee.PropertyName };
          b.Click += HeaderFilterButtonClick;
          StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
          stackPanel.Children.Add(new TextBlock() { Text = ee.PropertyName, VerticalAlignment = VerticalAlignment.Center });
          //stackPanel.Children.Add(new Button() { Content = "...", Name = "btn_" + ee.PropertyName });
          stackPanel.Children.Add(b);
          ee.Column.Header = stackPanel;
      };
  
      lb.Items.Add(e.Column.Header.ToString());
  }
  </code></pre>
  
  <p>thanks @mm8</p>
  
  <p>last problem:</p>
  
  <p>no button is created for the first column header and I don't know why:</p>
  
  <p><a href="https://i.stack.imgur.com/WvFrB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WvFrB.png" alt="enter image description here"></a></p>
  
  <p>ideas?</p>