How to concatenate strings in Xamarin.Forms?

社会主义新天地 提交于 2020-12-08 05:59:50

问题


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>


回答1:


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>



回答2:


You could use IValueConverter, which will accept Employee and will return full name.

Or you could use MultiComponentLabel. It allows you to bind couple different values to one Label.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Name="Page"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:controls="clr-namespace:SuperForms.Controls;assembly=SuperForms.Controls"
             x:Class="SuperForms.Samples.MultiComponentLabelPage">
  <controls:MultiComponentLabel Margin="0,20,0,0">
    <controls:MultiComponentLabel.Components>
      <controls:TextComponent Text="{Binding EMP_LAST_NAME}"/>
      <controls:TextComponent Text="{Binding EMP_FIRST_NAME}"/>
    </controls:MultiComponentLabel.Components>
  </controls:MultiComponentLabel>
</ContentPage>

Just use MultiComponentLabel instead of couple Labels

For your ListView

<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" />

    <controls:MultiComponentLabel Grid.Row="1" Grid.Column="1">
      <controls:MultiComponentLabel.Components>
        <controls:TextComponent Text="{Binding EMP_LAST_NAME}"/>
        <controls:TextComponent Text="{Binding EMP_FIRST_NAME}"/>
      </controls:MultiComponentLabel.Components>
    </controls:MultiComponentLabel>

  </Grid>
</ViewCell>




回答3:


 <Label Text="{Binding Value, StringFormat='string before value {0:F0} string after value'}"/>

Suppose your value is 1234. The output will be in this case:

string before value 1234 string after value




回答4:


Use FormattedText property in xamarin forms Label as follow:

<Label Grid.Column="1" Grid.Row="1">
    <Label.FormattedText>
        <FormattedString>
            <Span TextColor="White" FontSize="18" Text="{Binding EMP_LAST_NAME'}"/>
            <Span TextColor="White" FontSize="18" Text="{Binding EMP_FIRST_NAME}"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

Xamarin.Forms Label

And also you can add Style to avoid code duplication for TextColor, FontSize and other properties in your code.

Styling Xamarin.Forms Apps using XAML Styles



来源:https://stackoverflow.com/questions/38199133/how-to-concatenate-strings-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!