1

I have a winforms paint event handler that handles the paint event for a picturebox. As the paint event description says, "...the event is fired when the control is redrawn". I do not quite understand this and I wish to raise the same event in WPF on an Image control. But I can't find any such events. Here is the winforms code

How do I do this in WPF??

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (pictureBox1.Image != null)
    {
        if (temprect != new Rectangle())
        {
            e.Graphics.DrawRectangle(new Pen(selectionBrush, 2), temprect);
        }
    }
    else
    {
        using (Font myFont = new Font("Arial", 40, FontStyle.Bold))
        {
            e.Graphics.DrawString("No Image", myFont, Brushes.LightGray,
                new Point(pictureBox1.Width / 2 - 132, pictureBox1.Height / 2 - 50));
        }
    }
}

I have already converted all the code within the event Hanlder to WPF using DrawingContext class. Now, I only need help on the event that I can raise "when the Image control is redrawn".

mm8
  • 163,881
  • 10
  • 57
  • 88
Ken Janka
  • 121
  • 3
  • 19
  • Why do you want to have this event? You can create same `event` (use `EventHandler`) as in winforms or do you want wpf specific [routed event](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/routed-events-overview) ([difference](https://stackoverflow.com/q/3405093/1997232))? Or do you simply want to do [custom painted control](https://stackoverflow.com/q/13813753/1997232)? – Sinatr Jan 04 '19 at 09:48
  • Well as the description (not very informative) of the paint event says, "the event is raised when the picturebox is REDRAWN". So my problem is that I can't find any WPF equivalent event that is raised when a control is REDRAWN. I tried using the SourceUpdated event on Image control, but it's never raised. – Ken Janka Jan 04 '19 at 10:02
  • WPF uses different [architecture](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/wpf-architecture) for its UI than winforms. You didn't explain why do you want this event. To do what? If you simply want to draw a line or two, then adding visual or two is one possibility, those are often defined as control template in xaml (resource dictionary), but code behind is also good (see link in my previous comment). – Sinatr Jan 04 '19 at 10:14
  • Wpf Control provides events like "Initialized", "Loaded", "ManipulationCompleted". Just try out which one fires when you redraw. – Master Azazel Jan 04 '19 at 10:17

1 Answers1

1

WPF doesn't use WinForm's on-demand mode painting. The OnRender method of a UIElement is called by the layout system whenever it wants the element to "redraw" itself. You can override this method in your class:

public class YourElement : FrameworkElement
{
    protected override void OnRender(DrawingContext dc)
    {
        base.OnRender(dc);
    }
}

If you want to re-render the element explicitly you could call the InvalidateVisual() method.

mm8
  • 163,881
  • 10
  • 57
  • 88