How to create a WPF Button Dynamically?

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

private void CreateAButton()

{

    Button btn = new Button();

    btn.Height = 80;

    btn.Width = 150;

    btn.Content = "Click ME";

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

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

    LayoutRoot.Children.Add(btn);

}