E
E
Evgeny Ivanov2020-09-03 10:21:09
WPF
Evgeny Ivanov, 2020-09-03 10:21:09

How to set the attributes of a button created dynamically?

Visual Studio 2019 WPF XAML

There is working code.

Button btn = new Button();
btn.Height = 80;
btn.Width = 150;
btn.Name = "Button1";
btn.Content = "Click ME";
ActionStackPanel.Children.Add(btn);

The button is created on the ActionStackPanel (StackPanel) element.
As I understand it, it is simply impossible (or difficult) to create it on the window.

But I can't set the Margin (position) and Click (event) attributes.
//Всё ниже не верно
 btn.Click = "Button1_Click";
 btn.Click += "Button1_Click";
 btn.Margin.Left = 1;
 btn.Margin="1,0,0,0";


How to set these attributes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-09-03
@logpol32

btn.Margin = new Thickness(1,0,0,0);
 btn.Click += Button1_Click;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question