Background:
I am converting a control from WinForms to WPF. In WinForms I overrode a PictureBox and used the OnPaint event to draw what I needed to that control. That control would handle all the double-buffering and clipping.
I converted the control to WPF and am inheriting from Canvas now. I am overriding the OnRender event and drawing everything to the DrawingContext, but it looks like the DrawingContext allows me to draw past the control onto the rest of the view:

In the above image the solid dark pink box is the control and the light pink box and green line are drawn at (0, 0), but the blue box is drawn at (0, -100).
In WinForms the PictureBox would clip whatever was not in the control's region.
Also, I found that the blue box does get clipped if the box falls below the control. I assume that is because it does clip below the enclosing Grid, but I'm not sure.
Questions:
OnRender method of my
control to the specific region?I tried adding shapes to the Canvas children, but this is extremely slow.
The issue is related to how Canvas renders stuff. That means its children can be rendered outside its actual region. However you can avoid that default behavior by setting the property
ClipToBounds to true:
yourCanvas.ClipToBounds = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With