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>
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 .
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.
The simplest method of adding two strings in C# is using + or += operators.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With