title: How to delete dynamically created buttons
body: <p>I'm creating a WPF application in c# and i'm wondering how to delete dynamically created button in grid assuming that i know buttons name. </p>
  
  <p>Explanation: I have 16 buttons and i want to delete two buttons at the same time when i clicked one button. I Have their names but i don't know how to use this.</p>
  
  <p>this is what i tried but this is useful only when you're trying delete clicked button.</p>
  
  <pre><code>public void delete(Button x)
  {
      myGrid.Children.Remove(x);
  }
  </code></pre>
  
  <p>I hope you understand my question because my English is not my native language. </p>
  
  <p>My code:</p>
  
  <pre><code>for (int i=1; i&lt;17; i++) 
  {
      Button x = new Button();
      x.MinHeight = 100;
      x.MinWidth = 100;
      var brush = new ImageBrush();
      brush.ImageSource = new BitmapImage(new Uri(@"C:path\slikeSpomin\hrbtnaStran.png", UriKind.Relative));
      x.Background = brush;
      int ime = r.Next(0, max);
      //x.Content = pari[ime];
      x.Name = pari[ime];
      pari.RemoveAt(ime);
      max--;
      x.VerticalAlignment = VerticalAlignment.Top;
      x.HorizontalAlignment = HorizontalAlignment.Left;                
      x.Margin = new Thickness(a, b, 0, 0);
      a = a + 130;
      if (i % 4 == 0)
      {
          b = b + 120;
          a = 10;
      }
      x.Click += new RoutedEventHandler((obj, ev) =&gt; btnClick(x));
      gumbiDodani.Insert(0, x);
      gridSlike.Children.Add(x);
  
  } // Here i create my buttons
  
  private void btnClick(Button x)
  {
      var brush = new ImageBrush();
      string slika = x.Name;
      sliki.Add(slika);
      brush.ImageSource = new BitmapImage(new Uri(@"path\slikeSpomin\" + slika + ".png", UriKind.Relative));
      x.Background = brush;
      //zaženem metodo, ki preverja enake in se izvede samo v primeru, če so enake
      enake(slika, x);
  }
  List&lt;string&gt; ksad = new List&lt;string&gt;();
  public void enake(string ime, Button x)
  {
      ksad.Add(ime);
      if (ksad.Count == 2)
      {
          string prva = ksad[0];
          string druga = ksad[1];
          if (prva.Equals(druga + "a") || druga.Equals(prva + "a"))
          {
  
              string imeGumba = x.Name;
              MessageBox.Show(prva + "   " + druga);
              //this.gridSlike.Children.re
              gridSlike.Children.Remove(x);
              ksad.Clear();
          }
          else 
          {
              MessageBox.Show("happy");
              ksad.Clear();
          }
      }
  }
  </code></pre>