Q:

How can I position an Element using RelativeLayout with respect to an element not at the top?

I'm trying to use a relative layout to position multiple elements. The topmost element is positioned relative to parent, and the element immediately below that has its' YConstraint set by the height of the first element:
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView,
ElementName=TopElement,Property=Height, Factor=1, Constant=0}">

But let's say I want to add a third element, immediately below the second one. How would I set that YConstraint with a ConstraintExpression? Is there a way, in XAML, to set it based on the sum of the heights of the first two elements? If not, how can I achieve this? (I can't use a StackPanel)

A:

Unfortunately we cannot set such constraints in XAML. We can, however achieve it programmatically:
XAML:
<RelativeLayout x:Name="RelativeLayout" BackgroundColor="White">
    <BoxView x:Name="BoxView1" BackgroundColor="Red"
             RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Y, Factor=0, Constant=0}"
             RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.1, Constant=0}"
             RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=0}"/>

    <BoxView x:Name="BoxView2" BackgroundColor="Green"
             RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToView, ElementName=BoxView1, Property=Height, Factor=1, Constant=0}"
             RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.1, Constant=0}"
             RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1, Constant=0}"/>

    <BoxView x:Name="BoxView3" BackgroundColor="Blue"/>
</RelativeLayout>

Code-behind:
protected override void OnAppearing()
{
    base.OnAppearing();
    RelativeLayout.Children.Add(BoxView3, Constraint.RelativeToParent(parent =>
    {
        return parent.X;
    }), Constraint.RelativeToView(BoxView2, (parent, sibling) =>
    {
        return sibling.Y + sibling.Height;
    }), Constraint.RelativeToParent((parent) =>
    {
        return parent.Width;
    }), Constraint.RelativeToParent((parent) =>
    {
        return parent.Height * .1;
    }));
}

