0
votes

I'm working on a custom control in WPF that implements OnRender by calling a visit function with itself as the visitor. The control implements the visitor interface that draws lines, circles, etc. as appropriate. There are no child controls.

This all works, when the control renders I can see the primitives being rendered from my OnRender call.

However, what I'm struggling with is controlling the layout resulting from this. The Image control does exactly what I want to do. How do I replicate that behavior? Specifically:

  • If the user does not specify width or height, I want to set the rendered width/height of the control (either in my constructor or from another function that controls the layout).
  • If the user sets width or height to a specific value, I want to transform my drawing such that the aspect ratio of the control and drawing is preserved.

I'm trying to use the MeasureOverride functions to implement the behavior I want, but I'm not getting any results. My control is laid out with zero width/height, and then all my drawings get drawn on top of neighboring controls. Here is what I'm trying so far, hopefully this illustrates what I'm attempting to do:

protected override Size MeasureOverride(Size constraint)
{
    SymbolLayout symbol = new SymbolLayout(this);
    component.LayoutSymbol(symbol);

    Point b1 = MapToPoint(symbol.LowerBound);
    Point b2 = MapToPoint(symbol.UpperBound);
    return new Size(b2.X - b1.X, b2.Y - b1.Y);
}

I'm not even sure that MeasureOverride is the right function to be using to accomplish this...

1

1 Answers

0
votes

You also need to override the ArrangeOverride method. Layout in WPF is performed by a pair of recursive operations, Measure and Arrange. Measuring simply asks the visual tree what the required size of all the child controls is. This is done recursively, so if you had child controls, you would need to call Measure on all of your child elements as part of your Measure Override. Arranging is the second step in which the framework tells the control how much space it actually gets. Like Measuring, this is done recursively, and normally requires calling Arrange for each child control if there are any.