Share via

Binding with Enum .NET MAUI

Anonymous
2023-04-17T21:32:24.2166667+00:00

Hi, I have the following class:

namespace MyApp;

static class IconFont
{   
    // Religion
    public const string IconIslam = "\uf699";
    public const string IconChristianity = "\uf654";
    public const string IconJudaism = "\uf69a";

}

but in my database, I am returning these IDsL

Islam = 1 Christianity = 2 Judaism = 3

In my XAML I want to do the following:

<Image x:Name="ImageReligion" Grid.Column="1" WidthRequest="40" HeightRequest="40" Margin="10" HorizontalOptions="End" VerticalOptions="Center" />

Now in codebehind, how can I set the value that comes from the database and show the related IconFont? for example if I have the value as 1 from the database then the image should show IconIslam. something like this:

ImageReligion.Source = new FontImageSource() { FontFamily = "FontSolid", Glyph = IconFont.IconIslam, Color = Colors.Black };

Thanks, Jassim

Developer technologies | .NET | .NET Multi-platform App UI
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Anonymous
2023-04-18T03:09:04.67+00:00

Hello,

how can I set the value that comes from the database and show the related IconFont?

Based on your IconFont, it is not Enum , You can do it by adding a convertvalue method in the IconFont like following code.

static class IconFont
{
    // Religion
    public const string IconIslam = "\uf699";
    public const string IconChristianity = "\uf654";
    public const string IconJudaism = "\uf69a";
   public static string ConvertIntToIconFont(int glypeFromDB)
    {
        switch (glypeFromDB)
        {
                case 1: return IconIslam;
                case 2: return IconChristianity;
                case 3: return IconJudaism;
              default:
                return IconIslam;               
        }
    }
}

Then you can use this method to convert int from your DB to string from IconFont


//for testing.
 int glypeFromDB = 2;

ImageReligion.Source = new FontImageSource() { FontFamily = "FontSolid", Glyph =IconFont.ConvertIntToIconFont(glypeFromDB), Color = Colors.Black };

Best Regards, Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.