S
S
splunk2016-11-05 02:39:14
.NET
splunk, 2016-11-05 02:39:14

WITH#. How to add a button to a form from code?

How to add a button to a form from code? Not through xaml or handles.
Is there something to be passed to MainWindow()?

using System.Windows;
using System.Windows.Controls;

namespace Test1
{
    public partial class MainWindow : Window
    {
        public myButtonClass ttt = new myButtonClass();
        public MainWindow()
        {
            InitializeComponent();
            // как вывести кнопку ttt на форму???
        }
    }
   
public class myButtonClass
    {
        public Button newButton;
        public myButtonClass()
        {
            newButton = new Button();
            newButton.Content = "press me";
            newButton.Height = 50;
            newButton.Width = 50;
            newButton.Margin = new Thickness(50, 50, 0, 0);
            newButton.IsEnabled = true;
          }    
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rou1997, 2016-11-05
@Rou1997

This is not a "form", this is a window, in order to add a button to it, you need to contact the container (Layout manager) that processes the content (controls) in your window, for example, if it is Grid, then in my opinion this is how the (this.Content as Grid).Children.Add(button);
documentation will tell you more precisely.
Are you sure it's needed? What is the meaning of this "wrapper"? It complicates, in that you still need to pass the context (this) to it, it’s better to do it first directly in the MainWindow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question