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;
}
}
WindowTitle = "Canvas Sample"
'Create a Canvas as the root Panel
Dim myCanvas As New Canvas()
myCanvas.Background = Brushes.LightSteelBlue
Dim txt1 As New TextBlock
txt1.FontSize = 14
txt1.Text = "Hello World!"
Canvas.SetLeft(txt1, 10)
Canvas.SetTop(txt1, 100)
myCanvas.Children.Add(txt1)
'Add a second text element to show how absolute positioning works in a Canvas
Dim txt2 As New TextBlock
txt2.FontSize = 22
txt2.Text = "Isn't absolute positioning handy?"
Canvas.SetLeft(txt2, 75)
Canvas.SetTop(txt2, 200)
myCanvas.Children.Add(txt2)
Me.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>