ImageButton FontImageSource.Glyph and FontImageSource.Color binding in csharp

Alessandro Caliaro 4,186 Reputation points
2021-01-26T08:53:54.11+00:00

Hi

I have an ImageButton and I set a static source:

            iconCancellaOModificaQuantitaOBarcode.Source = new FontImageSource
            {
                Glyph = Library.IconaCancella,
                FontFamily = "ionicons",
                Color = Color.Red,
                Size = ICON_SIZE
            };

I would like to have Glyph and Color's FontImageSource binding, but I see only ImageButton.SourceProperty.
It seems that is possible in xaml:

      <ToolbarItem.IconImageSource>

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

        </ToolbarItem.IconImageSource>

Is not possible to bind Glyph and Color in csharp without bind all the "Source"?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,961 Reputation points
    2021-01-26T12:30:47.82+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    To set data binding in csharp, try using the BindableObjectExtensions.SetBinding method.

    Check the code:

       var iconSource = new FontImageSource  
       {  
           Size = ICON_SIZE,  
           FontFamily = "ionicons  
       };  
       iconSource.SetBinding(FontImageSource.GlyphProperty, "name");  
       iconSource.SetBinding(FontImageSource.ColorProperty, "name");  
         
       var imageButton = new ImageButton { Source = iconSource };  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,186 Reputation points
    2021-01-26T16:51:17.023+00:00
                this.imagebutton.Source = new FontImageSource() { FontFamily = "ionicons" };  
                this.imagebutton.Source.SetBinding(FontImageSource.GlyphProperty, "Glyph");  
                this.imagebutton.Source.SetBinding(FontImageSource.ColorProperty, "Color");  
    

    this works!

    Thanks @JarvanZhang

    0 comments No comments

Your answer

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