Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

wpf - Binding only part of a label

How would one achieve mixing bound values with constant text in a WPF bound control?

For example, say I have a form displaying orders, and I want a label that displays text like "Order ID 1234".

I've tried things like:

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

Is this achievable, or do I have to do something like having more than one label in a flow control?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label.
For example, the following sample will work:

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

The same as short Version:

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

Using it to display a unit after the value:

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

While this sample will not:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...