How to create a TextBox in WPF dynamically

The code listed in Listing 5 creates a TextBox control programmatically. First, it creates a TextBox object and sets its width, height, contents, background and foreground and later the TextBox is added to the LayoutRoot.

private void CreateATextBox()

{

    TextBox txtb= new TextBox();

    txtb.Height = 50;

    txtb.Width = 200;

    txtb.Text = "Text Box content";

    txtb.Background = new SolidColorBrush(Colors.Orange);

    txtb.Foreground = new SolidColorBrush(Colors.Black);

    LayoutRoot.Children.Add(txtb);

}

Listing 5