Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why Glyph property in FontImageSource doesn't bind?

igorkr_10igorkr_10 Member ✭✭✭✭
edited April 18 in Xamarin.Forms

Hi community.
I have a strange behavior with FontImageSource. I can't get property binded to Glyph.

<ContentPage.BindingContext>
        <documents:DocumentListViewModel />
    </ContentPage.BindingContext>
    <ContentPage.ToolbarItems>            
        <ToolbarItem Command="{Binding ChangeViewTypeCommand, Mode=OneWay}">
            <ToolbarItem.IconImageSource>
                <FontImageSource Glyph="{Binding ViewTypeGlyph}" Size="26" FontFamily="{x:StaticResource MaterialFontFamily}" Color="{DynamicResource PrimaryColor}"/>
            </ToolbarItem.IconImageSource>
        </ToolbarItem>
    </ContentPage.ToolbarItems>

public string ViewTypeGlyph
        {
            get
            {
                switch (this.ViewType)
                {
                    case ViewType.SmallRows:
                        return MaterialDesignIcons.ViewAgendaOutline;
                    case ViewType.BigRows:
                        return MaterialDesignIcons.CropSquare;
                    case ViewType.Grid:
                        return MaterialDesignIcons.ViewGridOutline;
                }
                return MaterialDesignIcons.ViewAgendaOutline;
            }
        }

I put a break point on the get block and it does not raise.

What am I missing?

Note: setting Glyph to static property works as well

<FontImageSource Glyph="{x:Static icons:MaterialDesignIcons.ViewAgendaOutline}" Size="26" FontFamily="{x:StaticResource MaterialFontFamily}" Color="{DynamicResource PrimaryColor}"/>

Answers

  • Tracy320Tracy320 Member ✭✭✭

    Uploading your code may help us more to help you solve the problem. :)

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited April 20

    @Tracy320 I have created a small repro project.
    There are 2 toolbar items (first is an Icon with Glyph from binding, second is an Icon with Glyph from static class) in ItemsPage.

    <ToolbarItem>
                <ToolbarItem.IconImageSource>
                    <FontImageSource Glyph="{Binding Glyph}" Size="26" FontFamily="{x:StaticResource MaterialFontFamily}" Color="White"/>
                </ToolbarItem.IconImageSource>
            </ToolbarItem>
    
            <ToolbarItem>
                <ToolbarItem.IconImageSource>
                    <FontImageSource Glyph="{x:Static local:MaterialIcons.ViewAgendaOutline}" Size="26" FontFamily="{x:StaticResource MaterialFontFamily}" Color="White"/>
                </ToolbarItem.IconImageSource>
            </ToolbarItem>
    

    So the result is:

    As you can see, an Icon with binding is not showing.

  • jezhjezh Member, Xamarin Team Xamurai

    Thank you for posting in Developer Community.I am doing some research to investigate your problem. And I’ll get back to you as soon as I have an update. Much appreciated for your patience.

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited April 24

    @jezh said:
    Thank you for posting in Developer Community.I am doing some research to investigate your problem. And I’ll get back to you as soon as I have an update. Much appreciated for your patience.

    Hi. Is there any updates on this issue? Thanks

  • luisbirdsluisbirds Member ✭✭
    edited August 26

    Hi, any updates on this? I can confirm this bug.

    Binding works properly on Labels for instante. But it simply does not work in Toolbar items.

    This works:

    <Label BackgroundColor="#424242"

            Padding="15"
            TextColor="White"
            FontSize="Large"
            FontFamily="{StaticResource FontAwesomeFreeSolid}"
            Text="{Binding SaveIconFont}">
    


    This does not:

            <ToolbarItem.IconImageSource>
    
                <FontImageSource
                        Glyph="{Binding SaveIconFont}"
                        FontFamily="{StaticResource FontAwesomeFreeSolid}"
                        Size="Large"> 
                </FontImageSource>
    
            </ToolbarItem.IconImageSource>
    

    In the c# code my SaveIconFont = "\uf09c"; So it's in the correct format. It just shows the box with a cross icon (not found or something).

    I'm afraid this looks like another half cooked feature. I've searched all over the net, I haven't found a single example binding a font to a ToolbarItem successfully.

    If you've found an answer please let me know.

  • Same issue here. Any updates?

  • SaskiaSaskia USMember ✭✭

    I don't know if you can use this, but I solved it by using a converter:

    item.SetBinding(ToolbarItem.IconImageSourceProperty, new Binding("ToolbarIcon", converter: new ToolbarIconValueConverter()));

    public class ToolbarIconValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Icon string to FontImageSource
            return new FontImageSource
            {
                FontFamily = (OnPlatform<string>)Application.Current.Resources["MaterialFontFamily"],
                Glyph = (string)value,
                Color = Color.White,
                Size = AppProperties.GetNamedSizeForImage(NamedSize.Default)
            };
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
  • maxkoshevoimaxkoshevoi Member ✭✭

    Having the same issue. Created a GitHub issue for this. Maybe it will get resolved faster this way:
    github com/xamarin/Xamarin.Forms/issues/12700 (cannot post links)

Sign In or Register to comment.