Aller au contenu

StackPanel

Empile ses enfants les uns après les autres, verticalement ou horizontalement.

Vertical (par défaut)

Aperçu · Spacing="8"
Titre Sous-titre
<StackPanel Spacing="8">
  <TextBlock Text="Titre" FontSize="20" FontWeight="Bold" />
  <TextBlock Text="Sous-titre" Opacity="0.7" />
  <Button Content="Action" />
</StackPanel>

Remarquez

Le bouton ne s'étire pas sur toute la largeur : Button a HorizontalAlignment="Left" par défaut dans Fluent. Le TextBlock, lui, est en Stretch.

Horizontal

Aperçu · Orientation="Horizontal"
<StackPanel Orientation="Horizontal" Spacing="8">
  <Button Content="Annuler" />
  <Button Content="Valider" Classes="accent" />
</StackPanel>

Barre de boutons alignée à droite

Aperçu↔ redimensionnez
<StackPanel Orientation="Horizontal" Spacing="8" HorizontalAlignment="Right">
  <Button Content="Annuler" />
  <Button Content="Enregistrer" Classes="accent" />
</StackPanel>

Icône + texte

Aperçu
🔔 3 nouvelles notifications
<StackPanel Orientation="Horizontal" Spacing="6">
  <TextBlock Text="🔔" VerticalAlignment="Center" />
  <TextBlock Text="3 nouvelles notifications" VerticalAlignment="Center" />
</StackPanel>

Imbriquer

Aperçu · StackPanel dans un StackPanel
Profil
Nom
E-mail
<StackPanel Spacing="16" Margin="24">
  <TextBlock Text="Profil" FontSize="22" FontWeight="SemiBold" />

  <StackPanel Spacing="4">
    <TextBlock Text="Nom" FontWeight="SemiBold" />
    <TextBox Text="Ada Lovelace" />
  </StackPanel>

  <StackPanel Spacing="4">
    <TextBlock Text="E-mail" FontWeight="SemiBold" />
    <TextBox Text="ada@exemple.fr" />
  </StackPanel>
</StackPanel>

Pièges

Pas de * dans un StackPanel

Un StackPanel offre une taille infinie dans le sens de l'empilement. Un enfant ne peut donc pas « prendre le reste de la place », et une ListBox dedans ne défile pas. Utilisez une Grid ou un DockPanel.

Aperçu · fenêtre de 160 px de haut, 12 tâches
Tâches
Tâche 1Tâche 2Tâche 3Tâche 4Tâche 5Tâche 6Tâche 7…
❌ StackPanel : la liste sort de la fenêtre, pas de barre de défilement
Tâches
Tâche 1Tâche 2Tâche 3Tâche 4Tâche 5Tâche 6Tâche 7Tâche 8Tâche 9Tâche 10Tâche 11Tâche 12
✅ Grid Auto,* : la liste prend le reste et défile (essayez)
<!-- ❌ La liste grandit sans fin, pas de scroll -->
<StackPanel>
  <TextBlock Text="Tâches" />
  <ListBox ItemsSource="{Binding Taches}" />
</StackPanel>

<!-- ✅ La liste prend le reste et défile -->
<Grid RowDefinitions="Auto,*">
  <TextBlock Text="Tâches" />
  <ListBox Grid.Row="1" ItemsSource="{Binding Taches}" />
</Grid>