Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in Xamarin.Forms?

I want my two strings to be diplayed on just a single line. Is it possible for it to appear like this:

Curry Stephen

using this code

Text="{Binding EMP_LAST_NAME + EMP_FIRST_NAME}" ? ? ?

I currently have this code. Thanks a lot.

<ListView ItemsSource="{Binding EmployeesList}"
        HasUnevenRows="True">
<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <Grid Padding="10" RowSpacing="10" ColumnSpacing="5">
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="Auto"/>
          <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <controls:CircleImage Source="icon.png"
               HeightRequest="66"
               HorizontalOptions="CenterAndExpand"
               Aspect="AspectFill"
               WidthRequest="66"
               Grid.RowSpan="2"
               />

        <Label Grid.Column="1"
              Grid.Row="1"
              Text="{Binding EMP_LAST_NAME}"
               TextColor="White"
               FontSize="18"
               Opacity="0.6"/>

        <Label Grid.Column="1"
              Grid.Row="1"
              Text="{Binding EMP_FIRST_NAME}"
               TextColor="White"
               FontSize="18"
               Opacity="0.6"/>



      </Grid>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

like image 682
Jaycee Evangelista Avatar asked Jul 05 '16 08:07

Jaycee Evangelista


People also ask

How do I concatenate strings in XAML?

string formattedString = FirstName + " " + LastName; // This is better performance-wise since a StringBuilder will be used internally, which means less objects will be created. string betterFormattedString = string .

How do you concatenate strings?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.

What is the best way to concatenate strings in C#?

The simplest method of adding two strings in C# is using + or += operators.


1 Answers

You can't bind to multiple properties on a View Element.

In this case you should create a new property which does the format you want and bind it to the View.

Example:

public class EmployeeViewModel
{
    public string FirstName { get; set; }    
    public string LastName { get; set; }    
    public string FullName => $"{FirstName} {LastName}";
}

Then in XAML:

<Label Text="{Binding FullName}"/>

Another approach:

As suggested in the comments we can also use FormattedText property in a Label:

<Label.FormattedText>
   <FormattedString>
     <Span Text="{Binding FirstName}" />
     <Span Text="{Binding LastName}"/>
   </FormattedString>
</Label.FormattedText>
like image 80
jzeferino Avatar answered Sep 26 '22 15:09

jzeferino