Aller au contenu

Texte

Afficher du texte : simple, mis en forme, sélectionnable.

TextBlock

Aperçu
BonjourTitreDiscretCouleur
<TextBlock Text="Bonjour" />
<TextBlock Text="Titre" FontSize="28" FontWeight="Bold" />
<TextBlock Text="Discret" Opacity="0.6" FontStyle="Italic" />
<TextBlock Text="Couleur" Foreground="#6D28D9" />

Retour à la ligne et troncature

Aperçu · même texte, Width="200"↔ redimensionnez
TextWrapping="NoWrap" (défaut)
Un texte assez long qui ne passera pas à la ligne.
TextWrapping="Wrap"
Un texte assez long qui passera à la ligne automatiquement.
TextTrimming="CharacterEllipsis"
Un texte trop long sera coupé avec des points de suspension
MaxLines="2" + WordEllipsis
Deux lignes maximum, puis coupure propre au mot près, même si le texte continue encore.
<TextBlock Width="200" TextWrapping="Wrap"
           Text="Un texte assez long qui passera à la ligne automatiquement." />

<TextBlock Width="200" TextTrimming="CharacterEllipsis"
           Text="Un texte trop long sera coupé avec des points de suspension" />

<TextBlock Width="200" TextWrapping="Wrap" MaxLines="2" TextTrimming="WordEllipsis"
           Text="Deux lignes maximum, puis coupure propre au mot près, même si le texte continue." />

Alignement et interlignage

<TextBlock TextAlignment="Center" LineHeight="24" TextWrapping="Wrap" Text="…" />
<TextBlock TextAlignment="Justify" TextWrapping="Wrap" Text="…" />

Décorations

<TextBlock Text="Souligné" TextDecorations="Underline" />
<TextBlock Text="Barré" TextDecorations="Strikethrough" />

Espacement des lettres v12

<TextBlock Text="MAJUSCULES" TextElement.LetterSpacing="2" />

Mise en forme partielle (Inlines)

Un seul TextBlock, plusieurs morceaux (Run, Bold, Span…) avec chacun leur style.

Aperçu
Texte gras, italique, souligné.
Rouge et grand. Un span imbriqué
<TextBlock TextWrapping="Wrap">
  Texte <Bold>gras</Bold>, <Italic>italique</Italic>, <Underline>souligné</Underline>.
  <LineBreak />
  <Run Text="Rouge" Foreground="Red" /> et
  <Run Text="grand" FontSize="20" />.
  <Span Foreground="#6D28D9">Un <Bold>span</Bold> imbriqué</Span>
</TextBlock>

Valeur liée au milieu d'une phrase

<TextBlock>
  <Run Text="Vous avez " />
  <Run Text="{Binding NombreMessages}" FontWeight="Bold" />
  <Run Text=" nouveaux messages." />
</TextBlock>

Mise en forme de nombres et dates

<TextBlock Text="{Binding Prix, StringFormat='{}{0:C2}'}" />          <!-- 12,50 € -->
<TextBlock Text="{Binding Date, StringFormat='{}{0:dddd d MMMM}'}" />  <!-- mardi 19 novembre -->
<TextBlock Text="{Binding Taux, StringFormat='Taux : {0:P0}'}" />     <!-- Taux : 42 % -->

SelectableTextBlock

Texte que l'utilisateur peut sélectionner et copier. Un TextBlock normal, lui, ne se sélectionne pas.

Aperçu · essayez de sélectionner
ID de commande : 2026-11-ABCD-42TextBlock : non sélectionnable
ID de commande : 2026-11-ABCD-42SelectableTextBlock
<SelectableTextBlock Text="ID de commande : 2026-11-ABCD-42" />

<SelectableTextBlock TextWrapping="Wrap" SelectionBrush="#C4B5FD">
  Sélectionnez <Bold>ce texte</Bold> pour le copier.
</SelectableTextBlock>

Label

Associe un texte à un champ ; la touche d'accès donne le focus au champ.

Aperçu · cliquez sur le libellé
<StackPanel Spacing="4">
  <Label Content="_Nom d'utilisateur" Target="{Binding #NomUtilisateur}" />
  <TextBox x:Name="NomUtilisateur" />
</StackPanel>

Alt+N place le curseur dans le champ.

Polices

<TextBlock Text="Monospace" FontFamily="Cascadia Code, Consolas, monospace" />
<TextBlock Text="Inter" FontFamily="fonts:Inter#Inter" />   <!-- fournie par WithInterFont() -->

Police embarquée

App.axaml
<Application.Resources>
  <FontFamily x:Key="TitreFont">avares://MonApp/Assets/Fonts#Poppins</FontFamily>
</Application.Resources>
<TextBlock Text="Titre" FontFamily="{StaticResource TitreFont}" FontWeight="SemiBold" />

Les fichiers .ttf / .otf doivent être déclarés en AvaloniaResource dans le .csproj.

Police par défaut de toute l'application

Program.cs
AppBuilder.Configure<App>()
    .UsePlatformDetect()
    .WithInterFont()
    .With(new FontManagerOptions
    {
        DefaultFamilyName = "avares://MonApp/Assets/Fonts#Poppins"
    });

Titres réutilisables

App.axaml
<Application.Styles>
  <FluentTheme />
  <Style Selector="TextBlock.h1">
    <Setter Property="FontSize" Value="28" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Margin" Value="0,0,0,8" />
  </Style>
  <Style Selector="TextBlock.muted">
    <Setter Property="Opacity" Value="0.65" />
  </Style>
</Application.Styles>
<TextBlock Classes="h1" Text="Paramètres" />
<TextBlock Classes="muted" Text="Modifiez vos préférences." />