質問

かつを混合行き値に一定のテキストコンポーネントのラインナップ結合はコントロールのツールか?

例えば、いい形で表示する、注文したいラベル表示するテキストのように"注文ID1234"のようです。

ったようなもの:

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

この達成可能であり、私のような複数のラベルフローコントロールのツールか?

役に立ちましたか?

解決

は3.5 SP1を使用している場合は、結合にStringFormatプロパティを使用することができます:

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

それ以外の場合は、コンバータを使用します:

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

StringFormatConverterIValueConverter付きます:

[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();
    }

これはトリックを行うだろう。

[編集TextするContentプロパティを変更]

他のヒント

Binding.StringFormatプロパティは、ラベル上では動作しません、あなたはラベルにContentStringFormatプロパティを使用する必要があります。
たとえば、次のサンプルでは動作します:

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

ショートバージョンと同じます:

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

の値の後に単位を表示するためにそれを使用した:

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

このサンプルではありませんが。

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

しばしば単に例のために一緒に多重テキストブロックを連鎖さ見落とさ

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

もう一つのアプローチは、モーバイルコンピューティングTextBlock複数の実行に重要で

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

..が結合素子を使用する必要があり追加 BindableRun クラスです。

更新 がある点をこの技術...見 こちらの

私は別のアプローチを発見しました。 @ Inferisのソリューションは、私のために動作しませんとLPCRoyの@私のためにエレガントではありません。

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

これは、私のお気に入りは、この時点でのITは柔軟かつ凝縮思われます。

Mikolajの答えを修正します。

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

FallbackValueは必須ではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top