RelativePanel
The RelativePanel control is a Panel which lets you layout elements by specifying where the go in relation to other elements and in relation to the panel.
By default, an element is positioned in the upper left corner of the panel.
Attached properties are used to control the layout of elements. This table shows those properties.
Panel alignment
Sibling alignment
Sibling position
AlignTopWithPanel
AlignTopWith
Above
AlignBottomWithPanel
AlignBottomWith
Below
AlignLeftWithPanel
AlignLeftWith
LeftOf
AlignRightWithPanel
AlignRightWith
RightOf
AlignHorizontalCenterWithPanel
AlignHorizontalCenterWith
​
AlignVerticalCenterWithPanel
AlignVerticalCenterWith
​
This XAML shows how to arrange elements in a RelativePanel.
1
<RelativePanel BorderBrush="Gray" BorderThickness="1">
2
<Rectangle x:Name="RedRect" Fill="Red" Height="44" Width="44"/>
3
<Rectangle x:Name="BlueRect" Fill="Blue"
4
Height="44" Width="88"
5
RelativePanel.RightOf="RedRect" />
6
​
7
<Rectangle x:Name="GreenRect" Fill="Green"
8
Height="44"
9
RelativePanel.Below="RedRect"
10
RelativePanel.AlignLeftWith="RedRect"
11
RelativePanel.AlignRightWith="BlueRect"/>
12
<Rectangle Fill="Orange"
13
RelativePanel.Below="GreenRect"
14
RelativePanel.AlignLeftWith="BlueRect"
15
RelativePanel.AlignRightWithPanel="True"
16
RelativePanel.AlignBottomWithPanel="True"/>
17
</RelativePanel>
Copied!
The result looks like this.
Relative panel
Here are a few things to note about the sizing of the rectangles:
    The red rectangle is given an explicit size of 44x44. It's placed in the upper left corner of the panel, which is the default position.
    The green rectangle is given an explicit height of 44. Its left side is aligned with the red rectangle, and its right side is aligned with the blue rectangle, which determines its width.
    The orange rectangle isn't given an explicit size. Its left side is aligned with the blue rectangle. Its right and bottom edges are aligned with the edge of the panel. Its size is determined by these alignments and it will resize as the panel resizes.

Reference

​RelativePanel​

Source code

​RelativePanel.cs​
Last modified 6mo ago
Export as PDF
Copy link