tags:

views:

1279

answers:

1

I'm using an Adorner in .NET 3.5, and I'm able to draw by overriding OnRender, but I need the ability to redraw the adorner to change its appearance.

Essentially I'm looking for a way to clear the drawing context and call OnRender again. What's the best way to do this, or is there a better approach?

public class MyAdorner : Adorner
{
    private Brush brush = Brushes.Red;

    public DragArrowAdorner(UIElement adornedElement) : base(adornedElement)
    {}

    public void RedrawWithBrush(Brush newBrush)
    {
        brush = newBrush;

        // redraw..?
    }

    protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
    {
        // some drawing code...
        drawingContext.DrawRectangle(
            brush, 
            null, 
            new Rect(AdornedElement.DesiredSize));
    }
}
+3  A: 

The answer to your question is use InvalidateVisual to cause the OnRender to be called again

However, I would suggest instead of doing custom drawing on OnRender yourself to use the standard styling and visual tree templating to build the actual visual of the adorner. This also means you can run standard XAML animations inside it with storyboards.

If you want to go with this approach, in your adorner class you need to:

  • in the constructor either call base.AddVisualChild() or create your own visuals collection with the visuals you want to show in the adorner
  • override ArrangeOverride(Size size) in order to arrange the children properly;
  • override VisualChildrenCount to return the number of children in the adorner visual tree;
  • override GetCisualChild(int index) to return a particular child.

You can take a look at the ResizingAdorner MSDN sample for more info.

Franci Penov