Question

Comment peut-on obtenir un mélange des valeurs liées avec du texte constant dans un contrôle lié WPF?

Par exemple, dire que j'ai une forme affichage des ordres, et je veux une étiquette qui affiche du texte comme « Numéro de commande 1234 ».

J'ai essayé des choses comme:

text="Order ID {Binding ....}"

Est-ce possible, ou dois-je faire quelque chose comme ayant plus d'une étiquette dans un contrôle de flux?

Était-ce utile?

La solution

Si vous utilisez 3.5 SP1, vous pouvez utiliser la propriété sur la StringFormat de liaison:

<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>

Dans le cas contraire, utilisez un convertisseur:

<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>

étant un StringFormatConverter IValueConverter:

[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : IValueConverter
{
    public string StringFormat { get; set; }

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture) {
         if (string.IsNullOrEmpty(StringFormat)) return "";
         return string.Format(StringFormat, value);
    }


    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

Ça va faire l'affaire.

[ Modifier : Modifiez la propriété à Text Content]

Autres conseils

La propriété Binding.StringFormat ne fonctionne pas sur les étiquettes, vous devez utiliser la propriété ContentStringFormat sur l'étiquette.
Par exemple, l'exemple suivant fonctionnera:

<Label>
    <Label.Content>
        <Binding Path="QuestionnaireName"/>
    </Label.Content>
    <Label.ContentStringFormat>
        Thank you for taking the {0} questionnaire
    </Label.ContentStringFormat>
</Label> 

La même chose que la version courte:

<Label Content="{Binding QuestionnaireName}" ContentStringFormat="Thank you for taking the {0} questionnaire" />

L'utiliser pour afficher une unité après la valeur:

<Label Content="{Binding Temperature}" ContentStringFormat="{}{0}°C" />

Bien que cet échantillon ne sera pas:

<Label>
    <Label.Content>
        <Binding Path="QuestionnaireName" StringFormat="Thank you for taking the {0} questionnaire"/>
    </Label.Content>            
</Label>

On oublie souvent enchaînant simplement plusieurs TextBlocks ensemble par exemple

<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />

Une autre approche consiste à utiliser un seul TextBlock avec plusieurs éléments Exécuter en son sein:

<TextBlock><Run>Hello</Run><Run>World</Run></TextBlock>

.. mais de se lier à un élément que vous devez utiliser ajouter une classe BindableRun .

Mise à jour Mais il y a quelques inconvénients à cette technique ... voir ici

J'ai trouvé une autre approche. @ La solution de Inferis ne fonctionne pas pour moi et @ est de LPCRoy pas élégant pour moi:

<Label Content="{Binding Path=Order.ID, FallbackValue=Placeholder}" ContentStringFormat="Order ID {0}">

Il est mon préféré en ce moment, il est souple et semble condensé.

modifié la réponse de Mikolaj.

<Label Content="{Binding Order.ID}" ContentStringFormat="Order ID {0}" />

FallbackValue n'est pas obligatoire.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top