Wednesday, 21 August 2013

WPF: Custom control layout

WPF: Custom control layout

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...

No comments:

Post a Comment