using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace addComponentByCode { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // wywołanie metody tworzącej kontrolki CreateControls(); } // metoda kreujaca kontrolki za pomocą kodu private void CreateControls() { // definiujemy kolumny ColumnDefinition colDef1 = new ColumnDefinition(); ColumnDefinition colDef2 = new ColumnDefinition(); // dodajemy kolumny do kontrolki grid1 grid1.ColumnDefinitions.Add(colDef1); grid1.ColumnDefinitions.Add(colDef2); // definiujemy wiersze RowDefinition rowDef1 = new RowDefinition(); RowDefinition rowDef2 = new RowDefinition(); RowDefinition rowDef3 = new RowDefinition(); // ustawiamy wysokość wierszy na 30 GridLength gridLength = new GridLength(30); rowDef1.Height = gridLength; rowDef2.Height = gridLength; rowDef3.Height = gridLength; // dodajemy wiersze do kontrolki grid1 grid1.RowDefinitions.Add(rowDef1); grid1.RowDefinitions.Add(rowDef2); grid1.RowDefinitions.Add(rowDef3); // dodajemy Label // definujemy obiekt Label Label firstNameLabel = new Label(); // ustawiamy pole Content firstNameLabel.Content = "Enter your first name:"; // dodajemy grid1.Children.Add(firstNameLabel); // dodajemy TextBox TextBox firstName = new TextBox(); firstName.Margin = new Thickness(0, 5, 10, 5); Grid.SetColumn(firstName, 1); grid1.Children.Add(firstName); // dodajemy Label Label lastNameLabel = new Label(); lastNameLabel.Content = "Enter your last name:"; Grid.SetRow(lastNameLabel, 1); grid1.Children.Add(lastNameLabel); // dodajemy TextBox TextBox lastName = new TextBox(); lastName.Margin = new Thickness(0, 5, 10, 5); Grid.SetColumn(lastName, 1); Grid.SetRow(lastName, 1); grid1.Children.Add(lastName); // dodajemy Button Button submit = new Button(); submit.Content = "View message"; Grid.SetRow(submit, 2); grid1.Children.Add(submit); // dodajemy Button Button clear = new Button(); clear.Content = "Clear Name"; Grid.SetRow(clear, 2); Grid.SetColumn(clear, 1); grid1.Children.Add(clear); } } }