GETah
GETah

Reputation: 21409

Invalidate own WPF control

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

Upvotes: 4

Views: 7217

Answers (2)

GazTheDestroyer
GazTheDestroyer

Reputation: 21241

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.

Upvotes: 4

Piotr Auguscik
Piotr Auguscik

Reputation: 3681

Maybe instead of using your own control, you can draw image with your map and set this image as source to standard control? Such operations like drawing are usually taking some time so its better to prepare image in background and then switch it with currently displayed.

Upvotes: 1

Related Questions