Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalidate own WPF control

Tags:

c#

wpf

I have a custom WPF control that I made a couple of days ago:

 public class MapContext : FrameworkElement{
     //....
      protected override void OnRender(DrawingContext dc) {
         // Draw the map
         if (mapDrawing != null) dc.DrawDrawing(mapDrawing);
 }

The mapDrawing drawing is updated in another thread where all the geometries to display are computed, the thread then updates the UI by calling InvalidateVisual():

Application.Current.Dispatcher.Invoke(DispatcherPriority.Render, new Action(delegate { InvalidateVisual(); }));

On InvalidateVisual, MSDN documentation says:

Invalidates the rendering of the element, and forces a complete new layout pass. OnRender is called after the layout cycle is completed.

This is not the behaviour I want as the MapContext control layout did not change. Only the drawing inside has changed.

Question Is there a proper way of forcing OnRender method to be called without doing a complete layout pass?

Thanks in advance

like image 286
GETah Avatar asked Oct 29 '25 16:10

GETah


1 Answers

No, there is no way to force a re-render without a layout pass. Since WPF uses a retained mode system, OnRender() does not work like the old WinAPI days. OnRender() simply stores a collection of drawing instructions, and WPF determines how and when to do the actual rendering.

If you need to change the look of your control independantly of sizing, I'd suggest you use something like a DrawingVisual, and using RenderOpen() to add your mapDrawing when you want it to change.

like image 144
GazTheDestroyer Avatar answered Oct 31 '25 07:10

GazTheDestroyer