Sunday, March 27, 2011

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!

From stackoverflow
  • 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.

    pearcewg : Somehow, my problem solved itself. However, this answer is excellent, because it is very low impact. I will likely add this to my code as a defensive measure.

0 comments:

Post a Comment