Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation inside an adorner (calling OnRender)

Tags:

c#

.net

wpf

adorner

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));
    }
}
like image 506
Jason Anderson Avatar asked Feb 24 '09 22:02

Jason Anderson


1 Answers

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.

like image 50
Franci Penov Avatar answered Oct 05 '22 04:10

Franci Penov