views:

373

answers:

2

I have 2 fields that I'd like to format into a TextBlock, example: "{0} of {1} hours used".

Currently have:

<TextBlock Text="{Binding HoursEntered}" />
<TextBlock Text=" of " />
<TextBlock Text="{Binding EstimatedHours}"  />
<TextBlock Text=" hours used "  />

Was looking at StringFormat for a single field, however this appears to only be available for WPF, not Silverlight:

<TextBlock Text="{Binding Path=HoursEntered, StringFormat='{0} of XX hours used'}"/>

I thought to use MultiBinding but this is not available in Silverlight 3 either?

How can I do a format string with multiple bound fields in Silverlight 3 xaml?

+1  A: 

you could put the text in a readonly string in your binding source

Public ReadOnly Property HoursUsedMessage() As String
    Get
        Return String.Format("{0} of {1} hours used", _hoursEntered, _estimatedHours)
    End Get
End Property

just make sure you also raise property notification for this property in the HoursEntered and EstimatedHours setters

qntmfred
Where does this code get placed? Data is coming from a RIA DomainDataSource. Does this get added in the client or server code?
rotary_engine
this would be a property on the object that you are binding to.
qntmfred
The object comes from Entity Framework. Added a partial class and property as suggested above. Works.
rotary_engine
A: 

If you want a more dynamic solution you can use a Converter. I made a small example, see the link below. I have used element binding for brevity, but it works with any data binding.

http://pastebin.com/f4465f5ae

Henrik Söderlund