TextBlock

De Banane Atomic
Révision datée du 9 octobre 2018 à 20:57 par Nicolas (discussion | contributions) (→‎Couleur)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
Aller à la navigationAller à la recherche

StringFormat

Permet de mettre en forme une chaine de caractères. Présent depuis le Framework .NET 3.5 SP1.

Xaml.svg
<TextBlock Text="{Binding Path=MaPropriété, StringFormat=Texte {0} texte}" />

<!-- \ et {} servent de caractère d'échappement après le signe = -->
<TextBlock Text="{Binding Path=MaPropriété, StringFormat=\{0\}}" />

<!-- Format d'une date -->
<TextBlock Text="{Binding Path=Date, StringFormat=\{0:d\}}" />
<TextBlock Text="{Binding Path=Date, StringFormat={}{0:dddd MMMM dd}}" />

MultiBinding

Permet de former un string composé de plusieurs Binding.

Xaml.svg
<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="Texte {0} texte {1} texte">
            <Binding Path="MaPropriété1" />
            <Binding Path="MaPropriété2" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
Si le StringFormat commence par { il faut le précéder de {}.
StringFormat="{}{0} - {1}"

Différent styles dans le même TextBlock

Xaml.svg
<TextBlock>
    <TextBlock.Inlines>
        <Run Text="{Binding Path=Timestamp, StringFormat={}{0} - }" />
        <Run FontWeight="DemiBold" Text="{Binding Path=Message}" />
    </TextBlock.Inlines>
</TextBlock>

Retour à la ligne

Xaml.svg
<TextBlock Text="Ligne 1 &#10; Ligne 2"</TextBlock>

<TextBlock>Ligne 1<LineBreak />Ligne 2</TextBlock>

Propriétés

Couleur

Xaml.svg
<TextBlock Foreground="Red" />
Cs.svg
tbk.Foreground = new SolidColorBrush(Colors.Red);

TextAlignement

Center, Justify, Left, Right

TextWrapping: retour à la ligne quand le texte déborde

Xaml.svg
<TextBlock TextWrapping="Wrap"></TextBlock>

TextTrimming: comment couper le texte quand il déborde

None le texte n'est pas coupé, il déborde. Valeur par défaut
CharacterEllipsis le texte est coupé et (...) sont affichés.
WordEllipsis le texte est coupé au dernier mot et (...) sont affichés.

TextDecorations: souligner

TextDecorations.jpg

Police / Font

FontFamily Segoe UI par défaut sur Windows 10
FontSize 12 par défaut
FontStretch Normal/Medium par défaut, Condensed, Expanded, ...
FontStyle Normal par défaut, Italic, Oblique
FontWeight Normal par défaut, Bold, Light, ...

Typographie

Sélection du texte

TextBlock ne permet pas de sélectionner du texte. Pour cela, il faut utiliser une TextBox.

Xaml.svg
<TextBox Background="Transparent"
         BorderThickness="0"
         Text="{Binding MyTextBinding, Mode=OneWay}"
         IsReadOnly="True"
         TextWrapping="Wrap" />

TextBlock vs Label

Label est un Control, il peut donc:

  • avoir un custom control template
  • contenir des données autres qu'un string
  • utiliser un DataTemplate

TextBlock hérite directement de FrameworkElement et n'est donc pas un Control.
Labelvstextblock classhierarchy.png

  • Label est grisé lorsqu'il est disable
  • Label supporte les access keys
  • TextBlock est plus léger