Forcing shape redraw after model property updated

Oct 19, 2012 at 7:44 AM

Hi,

In my case the "Text" property is not enough for my custom shapes: the text drawn on the shape depends upon several properties of the model assigned to this shape. To draw this text, I override the Draw() method of my shapes, for example:

public override void Draw(Graphics graphics)
{
    base.Draw(graphics);
    ProcessorModel pm = this.ModelObject as ProcessorModel;
    if (pm != null)
    {
        graphics.DrawString(string.Format("Type: {0}\r\nName: {1}", pm.ProcessorType, pm.Name),
            DrawingToolsCache.GetFont(this.CharacterStyle.FontFamily.Name, this.CharacterStyle.SizeInPoints),
            DrawingToolsCache.GetBrush(Color.Black),
            this.GetBoundingRectangle(true));
    }
}

When I change property Name (or ProcessorType) in the model, shape is not updated (redrawn) immediately. To redraw the shape, I must move focus away from the model's PropertyGrid (e.g. by clicking on the Diagram's area).

How can I achieve immediate redraw?!

Oct 19, 2012 at 8:16 AM

I found the solution:

public override void NotifyModelChanged(int modelPropertyId)
{
    base.NotifyModelChanged(modelPropertyId);
    this.Invalidate();
}

Coordinator
Oct 19, 2012 at 8:32 AM

Call shape.InvalidateDrawCache() followed by shape.Invalidate(). 

BTW:
Did you notice the Caption class? It handles most of the drawing/transforming stuff. It's method names are equivalent to the shape's method names in order to make it clear where to call which method. Have a look at the EntitySymbol shape in the SoftwareArchitectureShapes. This shape uses multiple captions, too.

Oct 19, 2012 at 9:29 AM

Thank you for the advice! However, for my purposes it will be enough to override the Draw() method. My shapes are not rotatable, so there's nothing difficult here - just a call to Graphics.DrawString().