XAML how to add a child to a button in c# [duplicate]

Solution for XAML how to add a child to a button in c# [duplicate]
is Given Below:

My goal is as follows.

I have a stack panel that is empty, and as the program progresses, I need to add new elements to it. They will all be following the format of having a button with a text block inside of it.

<Button>
  <Textblock/>
</Button>

I was able to successfully add just the text blocks as a child to the stack panel with the following code:

TextBlock deliveryTextBlock = CreateTextBlock(deliveryMessage.timeRecieved, deliveryMessage.callsign, deliveryMessage.content); // Textblock with properties + text
                                
homeWindow.InboundMessageLogPanel.Children.Add(deliveryTextBlock);

Then I tried to add the button into the code by creating a button object and doing button.Children but this is not a property that exists, so I was unable to add a child to the button.

I also tried to create a system where I assigned an incrementing number name value to the button, and then attempted to assign the child by using homeWindow[messageID].Children.Add(deliveryTextBlock ) , but that did not seem to work as it would give an error.

Can anyone help me figure out the be the proper way to push a button wrapping around my text block to the stack panel?

Let’s create a sample app, and add a new window. I named mine AddControls. Here’s the XAML:

<Window x:Class=“WPFSample001.AddControls“

xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation“

xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml“

Title=“AddControls“ Height=“114“ Width=“212“

>
<Button Name=“btnAddMore“ Click=“btnAddMore_Click“>Add Another</Button>

And here’s what it looks like:

wpf029

A few things I’d like to point out. I’ve given a name to the StackPanel, so it can be manipulated in our C# code. I also added a handler for the Click event for the button, btnAddMore_Click. I also named the button, although it really wasn’t necessary for this sample.

OK, make sure to build the app so intellisense will work, then let’s jump to the C# code behind. (You may get an error about the click handler for the button not being found, that’s fine just ignore it.)

all we have to do is create a button control and add it to the children collection of the StackPanel, like so:

public void btnAddMore_Click(object sender, RoutedEventArgs e)

{

  System.Windows.Controls.Button newBtn = new Button();

  newBtn.Content = “A New Button”;

  splMain.Children.Add(newBtn);

} 

Note in this example I used the fully qualified System.Windows.Controls, I did that just to be explicit where the Button came from. After a new button is created, I set it’s Content property, which in this case will be the text on the button.

In the final line, I add it to the children collection of the StackPanel. When you run it, and press the “Add Another” Button, you will see:

wpf030

Now we have a new problem. We’ve added a button to the StackPanel, but clicking on it does no good as we haven’t tied it to an event handler. Turns out that’s pretty easy too.

public void btnAddMore_Click(object sender, RoutedEventArgs e)

{

  System.Windows.Controls.Button newBtn = new Button();

  newBtn.Content = “A New Button”;

  newBtn.Click += new RoutedEventHandler(newBtn_Click);

  splMain.Children.Add(newBtn);

}

What I did was use a delegate. I added a new RoutedEventHandler and tied it to the click event. The code for the event is in a method named newBtn_Click:

private void newBtn_Click(object sender, RoutedEventArgs e)

{

  MessageBox.Show(“New Button Clicked!”, “I got pressed.”);

}

The signature for the event handler has to match the signature for a Click event, since that’s what we’re routing to. Here, all I do is display a message box just so you can see something got done. Run it again and press the Add Another button, and the “A New Button” should appear. Press the new button and you should see a message box.

wpf031

Finally, I want to be clear you can use this technique to add any sort of control. Here I’ve added a Label as well as a button:

public void btnAddMore_Click(object sender, RoutedEventArgs e)

{

  System.Windows.Controls.Button newBtn = new Button();

  newBtn.Content = “A New Button”;

  newBtn.Click += new RoutedEventHandler(newBtn_Click);

  splMain.Children.Add(newBtn);



  System.Windows.Controls.Label newLbl = new Label();

  newLbl.Content = “Hi Mom!”;

  splMain.Children.Add(newLbl);

}

wpf032

I could have created the entire form in C# (or VB.Net) code, adding the StackPanel directly to the “this” object. However, that gets a bit laborious, best monocular for stargazing uk here and I wouldn’t recommend it as a general practice.