laslavinco
laslavinco

Reputation: 352

I am trying to create new button in C# but i think my syntax is not proper

So this is my Syntax in the bottom, actually i am making a app launcher, whenever I'll select an exe file it will create a new launch button for it but after running this code i am not getting any response at all, i tried looking the code for making new button but it was same. This is my code

private void AddNewAppBtn_Click(object sender, RoutedEventArgs e)
{
    Functions.addApp();
    Button appButton = new Button();
    appButton.Content = "Click Me";
    appButton.Name = "ButtonA";
    var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
    stackPanel.Children.Add(appButton);
}

Upvotes: 1

Views: 72

Answers (2)

Red Wei
Red Wei

Reputation: 940

Just create the button inside the XAML, no code-behind. By doing this and only this, you can implement the MVVM pattern which make your maintainment will less pain.

XAML code: <Button Content="MyButton"></Button>

Or you want to create a button on cs file. It should be added on your main grid.

Upvotes: 1

Salah Akbari
Salah Akbari

Reputation: 39966

Because you haven't added your Stackpanel to your main Grid or other layout that you want to have the dynamically created Stackpanel. First give your main Grid a name like this:

XAML:

<Grid Name="mainGrid">

Then add this line at the end of your code:

mainGrid.Children.Add(stackPanel);

Upvotes: 1

Related Questions