简体   繁体   中英

How do I raise an event when a WPF control ( Image control) is redrawn?

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM