Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: Force Canvas to Invalidate or Repaint itself?

I have a Silverlight application which has on it a Canvas. On that Canvas, I dynamically "paint" a bunch of stuff, but adding controls to the canvas.

I have a button outside the Canvas area which clears the content. The objects are removed (successfully). However, the Canvas area does not refresh itself immediately; it currently requires a MouseOver or other event for the Canvas itself.

What is the simplest way to have an external object invalidate a Canvas? I'm sure I'm missing something simple!

like image 356
pearcewg Avatar asked Jan 30 '09 22:01

pearcewg


2 Answers

It's a bit grubby, but you could try changing the visibility to 'Visible' (even though it already is) of the Canvas, so:

myCanvas.Visibility = Visibility.Visible;

We've found that this forces as redraw, even if the actual value of myCanvas.Visible hasn't changed...

Give it a go, it's only a one liner that may fix things. Although I would expect the Canvas to be redrawing anyway if you're removing things from it.

like image 121
George Sealy Avatar answered Oct 23 '22 04:10

George Sealy


In case someone comes along after the fact like I did looking for this answer...

yourFrameworkElement.InvalidateArrange();
yourFrameworkElement.UpdateLayout();

worked for me.

Note: calling only this.yourFrameworkElement.UpdateLayout(); did not work for me. I had to call InvalidateArrange(). I called UpdateLayout() immediately thereafter for completeness - probably has zero real impact.

Also the myCanvas.Visibility = Visibility.Visible; trick did not work for me as I was trying to cause the LayoutUpdated event to fire.

like image 20
dFlat Avatar answered Oct 23 '22 05:10

dFlat