Formatting multiple bound field in one TextBlock in XAML
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}" />
<TextBloc开发者_StackOverflow中文版k 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?
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
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
Update for Silverlight 4: you can now use the String.Format option.
<Button Content=”{Binding username, StringFormat=’Log Out of \{0\} Account’}“/>
精彩评论