Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
En este ejemplo se muestra cómo crear y usar una instancia de Canvas.
Ejemplo
En el ejemplo siguiente se posicionan dos elementos explícitamente mediante los métodos TextBlock y SetTop de SetLeft. En el ejemplo también se asigna un Background color de LightSteelBlue
al Canvas.
Nota:
Cuando usas el Lenguaje de Marcado de Aplicaciones Extensibles (XAML) para colocar TextBlock elementos, usa las propiedades Top y Left.
private void CreateAndShowMainWindow()
{
// Create the application's main window
mainWindow = new Window();
// Create a canvas sized to fill the window
Canvas myCanvas = new Canvas();
myCanvas.Background = Brushes.LightSteelBlue;
// Add a "Hello World!" text element to the Canvas
TextBlock txt1 = new TextBlock();
txt1.FontSize = 14;
txt1.Text = "Hello World!";
Canvas.SetTop(txt1, 100);
Canvas.SetLeft(txt1, 10);
myCanvas.Children.Add(txt1);
// Add a second text element to show how absolute positioning works in a Canvas
TextBlock txt2 = new TextBlock();
txt2.FontSize = 22;
txt2.Text = "Isn't absolute positioning handy?";
Canvas.SetTop(txt2, 200);
Canvas.SetLeft(txt2, 75);
myCanvas.Children.Add(txt2);
mainWindow.Content = myCanvas;
mainWindow.Title = "Canvas Sample";
mainWindow.Show();
}
}
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
Consulte también
.NET Desktop feedback