Canvas et RelativePanel¶
Positionner par coordonnées, ou par rapport aux autres éléments.
Canvas¶
Positions absolues¶
<Canvas Width="300" Height="200" Background="#F5F3FF">
<Rectangle Canvas.Left="20" Canvas.Top="20" Width="80" Height="50" Fill="#A78BFA" />
<Ellipse Canvas.Right="20" Canvas.Top="40" Width="60" Height="60" Fill="#6D28D9" />
<TextBlock Canvas.Left="20" Canvas.Bottom="20" Text="Bas gauche" />
</Canvas>
Left/Top partent du coin haut-gauche ; Right/Bottom mesurent la distance au bord droit/bas.
Ordre d'affichage¶
ZIndex 2
ZIndex 1
<Canvas>
<Border Canvas.Left="0" Width="80" Height="80" Background="Orange" ZIndex="2" />
<Border Canvas.Left="40" Width="80" Height="80" Background="Purple" ZIndex="1" />
</Canvas>
ZIndex fonctionne dans tous les panels, pas seulement Canvas. Sans ZIndex, le dernier enfant est au-dessus.
Déplacer un élément en C¶
Déplacer à la souris¶
<Canvas x:Name="Zone" Background="Transparent" PointerMoved="OnPointerMoved">
<Ellipse x:Name="Curseur" Width="20" Height="20" Fill="#6D28D9" />
</Canvas>
private void OnPointerMoved(object? sender, PointerEventArgs e)
{
var p = e.GetPosition(Zone);
Canvas.SetLeft(Curseur, p.X - 10);
Canvas.SetTop(Curseur, p.Y - 10);
}
Note
Background="Transparent" est nécessaire pour que la zone vide reçoive les événements de pointeur.
Sans fond (null), les clics « passent à travers ».
RelativePanel¶
Placer par rapport au panel et aux voisins¶
Ada Lovelace
Mathématicienne
→
↓
Nom : RightOf=Avatar, AlignTopWith=Avatar
Métier : Below=Nom, AlignLeftWith=Nom
Bouton : AlignRight/BottomWithPanel
Métier : Below=Nom, AlignLeftWith=Nom
Bouton : AlignRight/BottomWithPanel
<RelativePanel Width="320" Height="160">
<Border x:Name="Avatar" Width="64" Height="64" CornerRadius="32" Background="#C4B5FD"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignVerticalCenterWithPanel="True" />
<TextBlock x:Name="Nom" Text="Ada Lovelace" FontWeight="Bold"
RelativePanel.RightOf="Avatar"
RelativePanel.AlignTopWith="Avatar" Margin="12,0,0,0" />
<TextBlock Text="Mathématicienne"
RelativePanel.Below="Nom"
RelativePanel.AlignLeftWith="Nom" />
<Button Content="Suivre"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignBottomWithPanel="True" />
</RelativePanel>
| Propriété | Effet |
|---|---|
RightOf, LeftOf, Above, Below |
à côté d'un élément nommé |
AlignLeftWith, AlignTopWith… |
aligné sur le bord d'un élément |
AlignLeftWithPanel, AlignBottomWithPanel… |
collé au bord du panel |
AlignHorizontalCenterWithPanel, AlignVerticalCenterWithPanel |
centré |