tags:

views:

459

answers:

1

I've created a decorator which draws some custom graphics in OnRender method. The graphics depend on the position of the object containing the decorator. I set the decorator via Template property through Style. The problem is that the OnRender method of the decorator is called only once when the template is applied. So when I change the position of the object and call InvalidateVisual() on that object the decorator is not re-rendered.

I've managed to workaround this by setting the template of the object to null and then back to the same template like this:

if (myObject.Template != null)
{
  ControlTemplate tmpTemplate = myObject.Template;
  myObject.Template = null;
  myObject.Template = tmpTemplate;
}

This does the trick but I'm sure this is not the way it should be done. What am I missing?

A: 

I suppose you might be invalidating the wrong visual. Try declaring the global variable within your object class to contain your decorator instance, override OnApplyTemplate method of decorated element and wire global variable with a decorator instance within the template by means of FindName method call. Something like: myDecorator = Template.FindName("PART_decorator", this) as MyDecoratorType (note that decorator in xaml template should have an x:Name defined as "PART_decorator" in this case).

After that you can call InvalidateVisual() against the decorator instance where appropriate in your code.

Hope that helps.

Denis Vuyka