Aller au contenu

Saisie

Champs texte, mots de passe, nombres, autocomplétion.

TextBox

Aperçu · tapez du texte : le placeholder disparaît
<TextBox PlaceholderText="Votre nom" />

v12

Watermark devient PlaceholderText, UseFloatingWatermark devient UseFloatingPlaceholder.

Libellé flottant

Aperçu · vide / rempli
vide : le texte est dans le champ
E-mailrempli : il remonte au-dessus
<TextBox PlaceholderText="E-mail" UseFloatingPlaceholder="True" />

Multiligne

Aperçu · AcceptsReturn="True"
<TextBox AcceptsReturn="True" TextWrapping="Wrap" Height="120"
         PlaceholderText="Votre message…" />

Limiter et transformer

<TextBox MaxLength="10" PlaceholderText="10 caractères max" />
<TextBox CharacterCasing="Upper" PlaceholderText="Code pays" />
<TextBox IsReadOnly="True" Text="Lecture seule" />
<TextBox TextAlignment="Right" Text="1 250,00" />

Bouton d'effacement et préfixe

Aperçu · InnerLeftContent / InnerRightContent
🔍✕gauche : 🔍 · droite : bouton ✕ (clearButton)
€droite : suffixe €
<TextBox Classes="clearButton" PlaceholderText="Rechercher">
  <TextBox.InnerLeftContent>
    <TextBlock Text="🔍" Margin="8,0,0,0" VerticalAlignment="Center" />
  </TextBox.InnerLeftContent>
</TextBox>

<TextBox PlaceholderText="Montant">
  <TextBox.InnerRightContent>
    <TextBlock Text="€" Margin="0,0,10,0" VerticalAlignment="Center" Opacity="0.6" />
  </TextBox.InnerRightContent>
</TextBox>

Réagir à la saisie

<TextBox x:Name="Recherche" TextChanged="OnRecherche" KeyDown="OnTouche" />
private void OnRecherche(object? sender, TextChangedEventArgs e)
    => Filtrer(Recherche.Text ?? "");

private void OnTouche(object? sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter) Lancer();
}

Manipuler le texte en code

Saisie.Focus();
Saisie.SelectAll();
Saisie.CaretIndex = Saisie.Text?.Length ?? 0;
Saisie.SelectedText;          // texte sélectionné
Saisie.Clear();

Mot de passe

Aperçu · PasswordChar="•" + revealPasswordButton
👁
<TextBox PasswordChar="•" Classes="revealPasswordButton" PlaceholderText="Mot de passe" />

revealPasswordButton ajoute l'œil pour afficher le mot de passe.

NumericUpDown

Un champ numérique avec boutons ▲▼ (et molette / flèches du clavier).

Aperçu · cliquez sur les flèches
<NumericUpDown Value="1" Minimum="1" Maximum="99" Increment="1" FormatString="0" />

<NumericUpDown Value="19.90" Increment="0.5" FormatString="C2" />

<NumericUpDown Value="50" Minimum="0" Maximum="100" FormatString="0'%'"
               ButtonSpinnerLocation="Left" />

Note

Value est de type decimal? : liez-le à une propriété decimal? ou decimal.

<NumericUpDown Value="{Binding Quantite}" ShowButtonSpinner="False" AllowSpin="True" />

AutoCompleteBox

Un champ texte qui propose des suggestions pendant la frappe.

Aperçu · tapez « L »

LyonLilleLorient
FilterMode="StartsWith"
<AutoCompleteBox x:Name="Ville" PlaceholderText="Ville" FilterMode="StartsWith"
                 MinimumPrefixLength="1" />
Ville.ItemsSource = new[] { "Paris", "Marseille", "Lyon", "Lille", "Lorient", "Toulouse" };
FilterMode Effet
StartsWith commence par (insensible à la casse)
Contains contient
ContainsOrdinal contient, sensible à la casse
Custom via ItemFilter

Objets et filtre personnalisé

Pays.ItemsSource = listePays;              // List<Pays>
Pays.ItemFilter = (texte, item) =>
    item is Pays p && p.Nom.Contains(texte ?? "", StringComparison.OrdinalIgnoreCase);
Pays.ItemSelector = (texte, item) => ((Pays)item).Nom;

MaskedTextBox

Le masque impose le format : les caractères fixes sont déjà là, on ne remplit que les trous.

Aperçu · Mask="00/00/0000" en cours de saisie
19/11/____
<MaskedTextBox Mask="00/00/0000" />          <!-- date -->
<MaskedTextBox Mask="00 00 00 00 00" />      <!-- téléphone FR -->
<MaskedTextBox Mask="LL-000-LL" />           <!-- plaque d'immatriculation -->
Caractère Accepte
0 chiffre obligatoire
9 chiffre optionnel
L lettre obligatoire
? lettre optionnelle
A alphanumérique

Formulaire complet

Aperçu
Inscription
Créer un compte
<StackPanel Spacing="12" MaxWidth="420" Margin="24">
  <TextBlock Text="Créer un compte" FontSize="22" FontWeight="Bold" />
  <TextBox PlaceholderText="Nom complet" UseFloatingPlaceholder="True" />
  <TextBox PlaceholderText="E-mail" UseFloatingPlaceholder="True" />
  <TextBox PlaceholderText="Mot de passe" UseFloatingPlaceholder="True"
           PasswordChar="•" Classes="revealPasswordButton" />
  <NumericUpDown Value="18" Minimum="18" Maximum="120" FormatString="0 'ans'" />
  <CheckBox Content="J'accepte les conditions" />
  <Button Content="Créer" Classes="accent" HorizontalAlignment="Stretch"
          HorizontalContentAlignment="Center" />
</StackPanel>