Search code examples
c#stringxamlconcatenationbinding-context

How to add additional text (or string) to a binding in xaml


I have this:

<Label Text="{Binding Height}" AbsoluteLayout.LayoutBounds=".9,.17,-1,-1" TextColor="White" AbsoluteLayout.LayoutFlags="PositionProportional" HorizontalTextAlignment="Center" HorizontalOptions="Center" FontSize="Medium"/>

Obviously {Binding Height} takes up the entire text.

The binding is currently a double, and needs to remain so, I just need to concat an 'm' at the end to represent meters.

I have tried {Binding Height} m and {Binding Height + m} but obviously xaml doesn't work the same way a regular string concatenation would work.


Solution

  • Try using StringFormat. Like this:

    Text="{Binding Height, StringFormat='{}{0}m'}"
    

    Edited for clarity:

    You can write anything you want after the {0} argument.

    For example, the above will produce values like 25m, 10m etc.

    You can write something like this if you like:

    Text="{Binding Height, StringFormat='{}{0} is a good number.'}"
    

    The above will produce, for example:

    10 is a good number.