views:

250

answers:

0

I have a UserControl looking like this:

<UserControl
    MaxHeight="32"
    MaxWidth="32"
    MinHeight="25"
    MinWidth="25">
    <DockPanel>
        <!-- some stuff -->
    </DockPanel>
</UserControl>

In addition to the min/max size constraint, I want the control always being painted with Width = Height. So i override MeasureOverride and ArrangeOverride:

protected override Size MeasureOverride(Size availableSize)
{
    var resultSize = new Size(0, 0);
    ((UIElement)Content).Measure(availableSize);
    var sideLength = Math.Min(((UIElement)Content).DesiredSize.Width, ((UIElement)Content).DesiredSize.Height);

    resultSize.Width = sideLength;
    resultSize.Height = sideLength;

    return resultSize;
}

protected override Size ArrangeOverride(Size finalSize)
{
    ((UIElement)Content).Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
    return finalSize;
}

I understand that I must call Measure and Arrange on every child of the UserControl. Since the DocPanel is the only child of my UserControl and (in my understanding) is stored in the Content property of the UserControl, I simply call Measure and Arrange on this Content property. However the UserControl is not displayed. What am I doing wrong?