1

I am trying to draw a non-static (variable width height x y) filled rectangle in WPF. I have experience with Winforms but I never used WPF before. This is what I would do in WinForm using GDI+ drawing calls.

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(_brush1, _rect1);
}

And also, I would call this.Invalidate() whenever I change my _rect1 properties.

All the "tutorials" I have seen on this matter are only drawing static non-filled rectangles inside .xaml files (or they don't mention where the code the show goes...).

I know WPF's painting works differently, I just don't understand how. I found a similar method protected override void OnRender(DrawingContext dc) and trying adding this on the default class I got when I made my project public partial class MainWindow : Window, but nothing is drawn on the screen. Another thing that concerns me, is that DrawingContext class has only a DrawRectangle class and there is no FillRectangle.

How can I draw something like this efficiently ?

dimitris93
  • 4,155
  • 11
  • 50
  • 86

2 Answers2

0

You don't want to override OnRender in WPF. Technically, you can, of course, but it brings a lot of problems and it's generally not recommended.

What I would do is have a panel on the window, ideally a Canvas, then create a Rectangle object which you can then manipulate from the code-behind (or, better yet, using data binding). If you put it in the Canvas, you can manipulate its size and position by changing its Width and Height and the attached properties Canvas.Left and Canvas.Top.

Without knowing your specific scenario it is difficult to offer more specific advice.

Edit: This related question has some good answers and examples.

vesan
  • 3,289
  • 22
  • 35
  • how will i access my canvas with code ? and how will the rectangle be filled ? – dimitris93 Apr 24 '15 at 05:49
  • `Rectangle` has properties like `Fill` and `Stroke` that define the colors. The linked question has good examples, though if you're a beginner in WPF, a lot of stuff in there will probably confuse you. Unfortunately what you're trying to do isn't the easiest thing to do in WPF. – vesan Apr 24 '15 at 05:53
0

It is not the way you should use WPF. It has another philosophy of creating graphics. But if you are looking for a method which is analogous to the the Windows Forms way you could use this:

protected override void OnRender(DrawingContext drawingContext)
{
    base.OnRender(drawingContext);

    Background = Brushes.Transparent;
    drawingContext.DrawRectangle(
       Brushes.BlueViolet, new Pen(Brushes.Chartreuse, 2), new Rect(10, 10, 1000, 1000));
}

As you see you have to set the background transparent.

Fratyx
  • 5,717
  • 1
  • 12
  • 22
  • I mention in my question that I want to do the drawing efficiently – dimitris93 Apr 24 '15 at 06:05
  • It is efficient as it is low level and so it is fast. If you are looking for a way which is efficient for the programmer you have to read about the basics of WPF: XAML, Shapes, Drawings, DrawingContext. It is very powerful but it needs some time to understand it in the beginning. – Fratyx Apr 24 '15 at 06:14