How to draw a rectangle on a WPF canvas How to draw a rectangle on a WPF canvas wpf wpf

How to draw a rectangle on a WPF canvas


This should draw your rectangle as a 200x200 black square, provided front_canvas is displayed correctly.

Why would this code not draw a rectangle?

The main reasons this would not draw are:

  • front_canvas is not visible
  • front_canvas is not in the visual tree and being displayed correctly
  • Some other FrameworkElement is obscuring front_canvas, at least the upper left corner.
  • There is another object in the canvas at a higher z order.

Note that you'd typically also want to set StrokeThickness if you want to see the Stroke you specify.


To View Rectangle you must specify the StrokeThickness and set any Integer value greater than zero:

rect.StrokeThickness=2;