Label Converter not working

Jassim Al Rahma 1,556 Reputation points
2023-04-05T12:57:35.7666667+00:00

Hi, I have a Label that I want to show a value when returned from the database otherwise I want to show the word Add I tried Converter as below but not working.

<ContentPage.Resources>
    <local:MyProfileLabelConverter  x:Key="MyProfileLabelConverter" />
</ContentPage.Resources>

Here is my Label:

<Label x:Name="LabelMyHair" Text="{Binding Source={x:Reference .}, Path=Text, Converter={StaticResource MyProfileLabelConverter}}" VerticalOptions="Center" />

and this is my Converter:

public class MyProfileLabelConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (string.IsNullOrWhiteSpace((string)value)) return "Add";
        else return value;

        // return (string)value == "Add" ? IconFont.IconAdd : IconFont.IconEdit;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();// oneway binding, this method will not be called.
    }

Thank you Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2023-04-05T19:19:58.2666667+00:00