I using Label.FormattedText
on Label
for ListView
s Cell
for multiple text on a single control. I want a line break and text coming from mvvm property. This how I want to format the text
Text=" linebreak property_text | "
This code trying but that gives an error in xaml.
<Label FontSize="Medium" Text="{Binding name}" >
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding name}"/>
<Span Text="{Binding balance, StringFormat='
 = {0:N}' | }" FontSize="Micro"/>
<Span Text="Insufficiant balance" TextColor="Red" FontSize="Micro"/>
</FormattedString>
</Label.FormattedText>
</Label>
It shows some syntax error here StringFormat='
 = {0:N}' | }
.
Below output I am looking for
CodePudding user response:
You must add this namespace on the xaml first:
xmlns:system="clr-namespace:System;assembly=netstandard"
And then in your formatted text use it like this:
<Label FontSize="Medium">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding Name}" />
<Span Text="{Binding balance}" FontSize="Micro" />
<Span Text=" | " />
<Span Text="{x:Static system:Environment.NewLine"} />
<Span Text="Insufficiant balance" TextColor="Red" FontSize="Micro"/>
</FormattedString>
</Label.FormattedText>
</Label>
CodePudding user response:
You can add your own value converter to do this too:
<ContentPage.Resources>
<ResourceDictionary>
<local:MyBalanceConverter x:Key="balanceConv" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<Label HorizontalOptions="Center"
HorizontalTextAlignment="Start"
VerticalOptions="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding CardName}" />
<Span Text="{Binding Balance, Converter={StaticResource balanceConv}}"
FontSize="Micro" />
<Span Text=" | "
FontSize="Micro" />
<Span Text="Insufficient Funds"
TextColor="Red"
FontSize="Micro" />
</FormattedString>
</Label.FormattedText>
</Label>
</ContentPage.Content>
Where the IValueConverter
conversion is defined:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("\n{0:C2}", (double)value);
}
This gives you: