Domanda

Come si potrebbe ottenere mescolando valori legati con testo costante in un controllo WPF legato?

Ad esempio, dire che ho un modulo che visualizza gli ordini, e io voglio un'etichetta che visualizza testo come "ID ordine 1234".

Ho provato cose come:

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

È questo realizzabile, o devo fare qualcosa come avere più di un'etichetta in un controllo di flusso?

È stato utile?

Soluzione

Se si sta utilizzando 3.5 SP1, è possibile utilizzare la proprietà StringFormat sul legame:

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

In caso contrario, utilizzare un convertitore:

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

Con StringFormatConverter essere un 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();
    }

Basta così il trucco.

[ Modifica : Modificare la Text proprietà per Content]

Altri suggerimenti

La proprietà Binding.StringFormat non funziona su etichette, è necessario utilizzare la proprietà ContentStringFormat sull'etichetta.
Ad esempio, il seguente esempio funziona:

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

Lo stesso di breve Versione:

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

Usando per visualizzare un'unità dopo il valore:

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

Anche se questo campione non sarà:

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

Spesso trascurato è semplicemente il concatenamento più TextBlocks insieme ad esempio

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

Un altro approccio è quello di utilizzare un unico TextBlock con più elementi Run all'interno di esso:

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

.. ma di legarsi ad un elemento è necessario utilizzare aggiungere un BindableRun classe .

Aggiorna Ma ci sono alcuni svantaggi di questa tecnica ... vedi qui

ho trovato un altro approccio. @ La soluzione di Inferis non funziona per me e @ LPCRoy di non è elegante per me:

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

E 'il mio preferito in questo momento, è sembra flessibile e condensato.

Modificata la risposta di Mikolaj.

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

FallbackValue non è un must.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top