tags:

views:

15

answers:

1

Hi I have added canvas in the xaml file. From code iam creating two rectangles and adding it to the canvas. Here the size of both the triangles are same. When I run the application both the triangles are coming one upon the other I mean they are overlapping. But when I add them to a Stack panel they are coming one after the other? Can anyone tell me how can I render two rectangles in my example one after the other without overlapping each other using Canvas?

Here is the sample code of my app;

Rectangle rect1 = new Rectangle();

rect1.Margin = new Thickness(1.5, 2, 1, 1);

rect1.Height = 40;

rect1.Width = 60;

rect1.Stroke = new SolidColorBrush(Colors.Black);

myCanvas1.Children.Add(rect1);

Rectangle rect2 = new Rectangle();

rect2.Height = 40;

rect2.Width = 60;

rect2.Stroke = new SolidColorBrush(Colors.Black);

myCanvas1.Children.Add(rect2);

Thanks in advance

Padma

+1  A: 

A StackPanel arranges its Child elements automatically so that they don't overlap. A Canvas doesn't, as it arranges its Child elements according to their Canvas.Left and Canvas.Top attached properties.

Assign your Rectangles those properties to arrange them manually how you like.

Ozan