Formatting multiple related fields in a single text block in XAML

I have 2 fields that I would like to format in a TextBlock, for example: "{0} of {1} hours is used."

Currently:

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

I looked at StringFormat for a single field, however, it seems this is only available for WPF, not for Silverlight:

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

I was thinking of using MultiBinding, but is this not possible in Silverlight 3?

How can I create a format string with several related fields in Silverlight 3 xaml?

+3
source share
3 answers

you can put text in readonly string in the binding source

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

, HoursEntered EstimatedHours.

+2

Update for Silverlight 4: Now you can use the String.Format parameter.

<Button Content="{Binding username, StringFormat=’Log Out of \{0\} Account’}"/>
0
source

Source: https://habr.com/ru/post/1728437/


All Articles