How to create a Canvas

This is how how can create a Canvas in C# code behind.

In this code, RootLayoout is the root panel or main window, whatever you have on your page.

private void CreateAndShowCanvas()
{

// Create a canvas sized to fill the window
Canvas myCanvas = new Canvas();
myCanvas.Background = Brushes.LightSteelBlue;

// Add a TextBlock
TextBlock TextBlock1= new TextBlock();
TextBlock1.FontSize = 12;
TextBlock1.Text = "First Text Block!";
Canvas.SetTop(TextBlock1, 100);
Canvas.SetLeft(TextBlock1, 10);
myCanvas.Children.Add(TextBlock1);

// Add a second TextBlock
TextBlock TextBlock2= new TextBlock();
TextBlock2.FontSize = 18;
TextBlock2.Text = "TextBlock 2";
Canvas.SetTop(TextBlock2, 200);
Canvas.SetLeft(TextBlock2, 75);
myCanvas.Children.Add(TextBlock2);
RootLayout.Content = myCanvas;

}
}
Here is how to create a Canvas in XAML and set its properties.

Put this code within your Window or Page tag in your WPF application.



<Canvas Background="LightSteelBlue">
<TextBlock FontSize="12" Canvas.Top="100" Canvas.Left="10">First Text Block!</TextBlock>
<TextBlock FontSize="18" Canvas.Top="200" Canvas.Left="75">TextBlock 2</TextBlock>
</Canvas>