0

Hi I want to have a rectangle like the below picture to fill the entire canvas with different sizes and at different position scatter through out the canvas. My solution environment is WPF C#

rectangle with red background

Could some one please guide me of how to do it? Till now what I have done is canvas_loaded

Rectangle rect = new Rectangle(); 
rect.Fill = new SolidColorBrush(Colors.Black);
rect.Width = 100;
rect.Height = 100;
rect.Stroke = new SolidColorBrush(Colors.Black);

But the problem here how will I position it to the different locaion of the canvas, the size and width I can provide at run time with different value but I need to position the rectangles (Square) at diffrent XY co-ordinates so that none of the rectangles or Squares overlapp each other.

Please help.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Debhere
  • 1,055
  • 2
  • 19
  • 41

2 Answers2

0

You can use

Canvas.SetLeft(rect, <offset>) Canvas.SetRight(...), Canvas.SetTop(...), Canvas.SetBottom(...)

to position UIElement in the Canvas container.

vitaliy zadorozhnyy
  • 1,226
  • 10
  • 12
0

Use Random class to generate the xy co-ordinates

Random r=new Random();
r.Next(1,100);
Nithin Nayagam
  • 458
  • 2
  • 5
  • 9