AathifMahir / MauiIcons

MauiIcons is Icon Collection Library for .Net Maui
MIT License
200 stars 13 forks source link

Icon binding with MVVM not working #117

Open ricavir11 opened 3 weeks ago

ricavir11 commented 3 weeks ago

Icon is not displayed when a binding is applied with MVVM.

ViewModel example :

private FontAwesomeSolidIcons _myIcon;
public FontAwesomeSolidIcons MyIcon
{
            get => _myIcon;
            set
            {
                _myIcon = value;
                RaisePropertyChanged(() => MyIcon);
            }
}

public async Task PageAppearingAsync()
{
             MyIcon = FontAwesomeSolidIcons.AddressBook;
}

Related view component (Button)

<Button    
    Command="{Binding SearchAddressCommand}"
    Text="{mi:FontAwesomeSolid BindingContext={x:Reference HomeViewPage}, Icon={Binding MyIcon}, IconColor={DynamicResource 
   PrimaryColor}, IconSize=25}" />
AathifMahir commented 3 weeks ago

Icon is not displayed when a binding is applied with MVVM.

ViewModel example :

private FontAwesomeSolidIcons _myIcon;
public FontAwesomeSolidIcons MyIcon
{
            get => _myIcon;
            set
            {
                _myIcon = value;
                RaisePropertyChanged(() => MyIcon);
            }
}

public async Task PageAppearingAsync()
{
             MyIcon = FontAwesomeSolidIcons.AddressBook;
}

Related view component (Button)

<Button    
    Command="{Binding SearchAddressCommand}"
    Text="{mi:FontAwesomeSolid BindingContext={x:Reference HomeViewPage}, Icon={Binding MyIcon}, IconColor={DynamicResource 
   PrimaryColor}, IconSize=25}" />

Would mind sharing a repro sample on this, in our testing binding just works fine.

ricavir11 commented 3 weeks ago

Yes I will try to provide it soon. In my current project I'm using MVVM Helpers library but I will provide an example without it. Nevertheless, it would be interesting to provide a simple sample with MVVM bindings

vonmbs commented 3 weeks ago

Hey i had the same problem. I fixed it by adding the MVVM BindingContext to the XAML and reference it by name instead of adding it in the code behind.

Example:

<ContentPage ...>
  <ContentPage.BindingContext>
      <local:MainViewModel x:Name="MainViewModelName" />
  </ContentPage.BindingContext>
  ...
  <mi:MauiIcon Icon="{mi:FontAwesomeSolid Icon={Binding MyIcon},BindingContext={x:Reference MainViewModelName}}" />
  ...
</ContentPage>
AathifMahir commented 2 weeks ago

We have made a some improvements to overall binding support with v4 and that makes this no longer issue at all.

Additionally, we are final stage of the release. Stay tuned for v4