How to do Binding to the "Icon" property of FontAwesome UWP?

1

I have this gallery of icons that I downloaded for my UWP project

link

I need to do a Binding to Icon's property so the xaml called it that way:

xmlns:fa="using:FontAwesome.UWP"

and in the grid or in a stackpanel I call it this way:

<StackPanel>
   <Viewbox>
       <fa:FontAwesome Icon="Industry"/>
   </Viewbox>
</StackPanel>

How can I do binding, I thought that sending a string worked directly but not

    
asked by Wilmilcard 19.11.2018 в 14:50
source

2 answers

0

Ready, @Karel gave me the idea to solve it:

First create a class where you will convert the text into icon:

public class TextToIcon : IValueConverter
{
    public object Convert(object value, Type targetType = null, object parameter = null, string language = null)
    {
        FontAwesomeIcon item;
        if (value != null && Enum.TryParse(value.ToString(), out item))
            return item;
        else
            return FontAwesomeIcon.None;
     }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}

Then I add it to a Resource Dictionary in XAML:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="using:Proyecto.Converters"> <!-- este es el using del namespace de la clase convertidora -->

    <c:ImageToVisible       x:Key="ImageToVisible.conv"/>

</ResourceDictionary>

And now on the page, where I am editing the view, I do so:

<StackPanel>
    <Viewbox MaxHeight="35" Width="70">
          <fa:FontAwesome Icon="{x:Bind Icono, Converter={StaticResource TextToIcon.conv}}" Foreground="#f1c40f"/>
    </Viewbox>
</StackPanel>

And now the Binding is full on the Icon

    
answered by 20.11.2018 / 17:24
source
1

For UWP it is a bit different from WPF. In WPF there is the possibility of assigning a binding path that is string, but in UWP the type is an enum. So a possible solution is to declare the property of the viewmodel that you will link of type FontAwesome.UWP.FontAwesomeIcon .

Option 1

private FontAwesomeIcon _iconName;
public FontAwesomeIcon IconName
{
    get => _iconName;
    private set
    {
        if (value == _iconName) return;
        _iconName = value;
        OnPropertyChanged();
    }
}

And assuming you have namespace fa registered for the nuget of FontAwesome.UWP . Something like this:

xmlns:fa="using:FontAwesome.UWP"

Then you can link it like this in the view:

<StackPanel>
   <Viewbox>
       <fa:FontAwesome Icon="{Binding IconName}" />
   </Viewbox>
</StackPanel>

Option 2: Using a IValueConverter :

The other possibility is to pass a string (as you were thinking) and use converter to transform that value to the correct instance of FontAwesomeIcon .

The first one is easier, as long as you can get the icons you need directly from the enum, but if you need help with this one ... it warns: -)

I hope it serves you!

    
answered by 19.11.2018 в 17:38