title: Wpf button click in xaml.cs
body: <p>I'm currently working on the following code.</p>
  
  <pre><code>public void CbParty_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          spPartijen.Children.Clear();
          MySqlConnection conn = new MySqlConnection("Server=localhost; Database=imtenb; Uid=root; Pwd=");
          conn.Open();
          MySqlCommand command = conn.CreateCommand();
          command.CommandText = "SELECT * FROM kandidaten WHERE partij='" + ((DataRowView)cbParty.SelectedItem)["naam"].ToString() + "' ";
          MySqlDataReader reader = command.ExecuteReader();
          DataTable dtData = new DataTable();
          dtData.Load(reader);
  
          MySqlDataAdapter da = new MySqlDataAdapter(command);
          da.Fill(dtData);
  
  
          foreach (DataRow dr in dtData.Rows)
          {
              TextBox tbId = new TextBox();
              tbId.Text = dr["id"].ToString();
              tbId.Name = "tbId";
  
              TextBox tb = new TextBox();
              tb.Text = dr["kandidaat"].ToString();
              tb.Name = "tb";
  
              Button button = new Button();
              button.Content = "wijzigen";
              button.Click += ChangeKandidaat_Click;
  
              spPartijen.Children.Add(tb);
              spPartijen.Children.Add(button);
          }
      }
  
      public void ChangeKandidaat_Click(object sender, RoutedEventArgs e)
      {
          conn.ChangeKandidaat(tbId.Text, tb.Text, ((DataRowView)cbParty.SelectedItem)["naam"].ToString());
      }
  
  
  public void ChangeKandidaat(string id, string kandidaat, string partij)
      {
          try
          {
              conn.Open();
              MySqlCommand command = conn.CreateCommand();
              command.CommandText = "UPDATE kandidaten SET kandidaat='kandiddddaat' WHERE partij='@partij' and id='@id'";
              command.Parameters.AddWithValue("@id", id);
              command.Parameters.AddWithValue("@kandidaat", kandidaat);
              command.Parameters.AddWithValue("@partij", partij);
              command.ExecuteNonQuery();
              MessageBox.Show("De informatie is succesvol gewijzigd");
          }
          catch (Exception)
          {
              MessageBox.Show("Er is iets mis gegaan met het toevoegen van je standpunt, probeer het opnieuw.");
          }
  
          conn.Close();
      }
  </code></pre>
  
  <p>My problem is that the ChangeKandidaat_Click cannot see the tbId.Text and tb.Text, because is isn't maked yet.</p>
  
  <p>Maybe someone has a solution for me. Or am I doing this kind of thing wrong.
  I'm pretty new to wpf and C#.</p>
  
  <p>thank you in advance!</p>