0

I'm trying to move this WinForms code to WPF, but there is no Paint event.

private void OnPaint(object sender, PaintEventArgs e)
{
    var region = new Region(new Rectangle(0, 0, this.Width, this.Height));
    var rectangle = new Rectangle(0, 0, 50, 50);
    region.Xor(rectangle);
    e.Graphics.FillRegion(Brushes.Black, region);
}
Brian
  • 5,039
  • 7
  • 35
  • 45

1 Answers1

1

WPF doesn't work like WinForms in terms of graphics. You can't actually draw shapes, you have to place them into your content.

Geometry should serve as a good replacement for Region. You can use Geometry.Combine and specify GeometryCombineMode.Xor to replicate your drawing code.

RectangleGeometry is how you make rectangles. There are similar classes for other shapes.

To actually display the Geometry, put it in a Path, which can be used as a control's content.

Kendall Frey
  • 41,292
  • 18
  • 105
  • 145
  • You can actually draw shapes (as well as Geometry objects) using the `OnRender` override. That's how `Path` does it. The important thing to remember is that WPF uses retained mode graphics (opposed to immediate mode of GDI). – Eli Arbel Oct 03 '13 at 13:11