Can we concatenate Binding with StringFormat in XAML?

voidstreamvoidstream FRMember ✭✭✭
edited January 2017 in Xamarin.Forms

Hello,

Can we concatenate Binding values with stringformat IN XAML?

For example:

<label Text={Binding Firstname, Binding Lastname, StringFormat='{0} {1}'}/>

Best regards

Best Answers

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @TonyPinot

    Yes you can. I usually set up some common format strings in app-scoped resources for consistent re-use.

    For example formatting for 2, 3, 4 decimal places. Or for the date so changing the format app-wide requires only one change in on place.

    <Label BindingContext="{x:Reference this}"
                                   Text="{Binding Gps.Latitude,
                                                  StringFormat={StaticResource formatdecimalfourplaces}}">
    
     <x:String x:Key="formatdecimalfourplaces">{0:0.0000}</x:String>
    <x:String x:Key="formatdate">{0:M/d/yy}</x:String>
    
  • voidstreamvoidstream FRMember ✭✭✭
    edited January 2017

    @ClintStLaurent

    Hum,

    I'm not sure but i think you don't understand my question. I will take your example, can you concatenate Gps.Latitude and Gps.Longitude with StringFormat? In your example you just show me how create global string in XAML for re-use it?

    Best regards

  • voidstreamvoidstream FRMember ✭✭✭

    @batmaci said:
    @voidstream this is what you are looking for i guess

    Wow !

  • joseantonyjoseantony USMember

    @batmaci can we do that in a button.
    Text="{Binding OldValue,StringFormat={x:Static i18n:Strings.RecommendedValue}'{0}'}"

  • voidstreamvoidstream FRMember ✭✭✭

    @joseantony said:
    @batmaci can we do that in a button.
    Text="{Binding OldValue,StringFormat={x:Static i18n:Strings.RecommendedValue}'{0}'}"

    This XAML work's for me:

    <Label Text="{Binding StartDate, StringFormat={markupExtensions:Translate Format_Time}}"/>
    

    Format_Time value is:

    {0:HH\:mm}
    

    Add this XAML line to the current ContentPage parameters

    xmlns:markupExtensions="clr-namespace:YOURSOLUTIONNAME.MarkupExtensions"
    

    I'm using RESX

    https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/localization/

  • theocangtheocang Member ✭✭✭

    @voidstream Hey I use exacly the same fonctionnalies and it didn't work I can't use markupExtensions:Translate with {0}.

    So did you have ab idea ?

    Thanks

  • voidstreamvoidstream FRMember ✭✭✭

    @theocang said:
    @voidstream Hey I use exacly the same fonctionnalies and it didn't work I can't use markupExtensions:Translate with {0}.

    So did you have ab idea ?

    Thanks

    Are you using RESX? RESX Translation is working? Can you show your xaml code?

Sign In or Register to comment.