How to create a WPF Label Dynamically

The Label class in WPF represents Label control.

The following code snippet uses Label class to create a Label control in WPF.

private void CreateLabelDynamically()

{

    Label dynamicLabel = new Label();

    dynamicLabel.Name = "McLabel";

    dynamicLabel.Width = 240;

    dynamicLabel.Height = 30;

    dynamicLabel.Foreground = new SolidColorBrush(Colors.White);

    dynamicLabel.Background = new SolidColorBrush(Colors.Black);

 

    LayoutRoot.Children.Add(dynamicLabel);

}