title: Add Button to ListView dynamically in each row using C#
body: <p>I have a <code>ListView</code> in which I have to show all rows fetched from my database table. Again I have to give two edit and delete buttons in each row. As I am beginner for the same, I tried the following code in XAML-</p>
  
  <pre><code>&lt;ListView Height="352" HorizontalAlignment="Left" Margin="20,90,0,0" Name="Tab1lstProductCategories" VerticalAlignment="Top" Width="1008"&gt;
                          &lt;ListView.View&gt;
                              &lt;GridView&gt;
                                  &lt;GridViewColumn Header="Category Name" DisplayMemberBinding="{Binding Col1}" Width="200"/&gt;
                                  &lt;GridViewColumn Header="Created Date" DisplayMemberBinding="{Binding Col2}" Width="200"/&gt;
                                  &lt;GridViewColumn Header="Last Updated" DisplayMemberBinding="{Binding Col3}" Width="200"/&gt;
                                  &lt;GridViewColumn Header="Edit" Width="200"&gt;
                                      &lt;GridViewColumn.CellTemplate&gt;
                                          &lt;DataTemplate&gt;
                                              &lt;Button Content="Edit" Click="EditCategory" CommandParameter="{Binding Id}"/&gt;
                                          &lt;/DataTemplate&gt;
                                      &lt;/GridViewColumn.CellTemplate&gt;
                                  &lt;/GridViewColumn&gt;
                                  &lt;GridViewColumn Header="Delete" Width="200"&gt;
                                      &lt;GridViewColumn.CellTemplate&gt;
                                          &lt;DataTemplate&gt;
                                              &lt;Button Content="Delete" Click="DeleteCategory"/&gt;
                                          &lt;/DataTemplate&gt;
                                      &lt;/GridViewColumn.CellTemplate&gt;
                                  &lt;/GridViewColumn&gt;
                              &lt;/GridView&gt;
                          &lt;/ListView.View&gt;
                      &lt;/ListView&gt;
  </code></pre>
  
  <p>And in C# I Wrote-</p>
  
  <pre><code> DataTable dt=db.Select("select * from ProductCategories");
             for (int i = 0; i &lt; dt.Rows.Count; i++)
          {
              //Button edit, delete;
              //edit = new Button();
              //edit.Content="Edit";
              //edit.Click += EditCategory;
              //delete= new Button();
              //delete.Content="Delete";
              //delete.Click += DeleteCategory;
              //edit.CommandParameter = dt.Rows[i]["Id"];
              //delete.CommandParameter = dt.Rows[i]["Id"];
              Tab1lstProductCategories.Items.Add(new { Col1 = dt.Rows[i]["CategoryName"], Col2 = dt.Rows[i]["CreatedDate"], Col3=dt.Rows[i]["LastUpdated"]});                
          }
  
  
  
  private void EditCategory(object sender, RoutedEventArgs e)
          {
              Button b=sender as Button;
              MessageBox.Show(b.CommandParameter.ToString());
          }
  
      private void DeleteCategory(object sender, RoutedEventArgs e)
      {
      }
  </code></pre>
  
  <p>I got the result as-
  <img src="https://i.stack.imgur.com/8Gfyg.jpg" alt="enter image description here"></p>
  
  <p>I want button over there, Please help me out.</p>