androidxamlxamarin.formsiconstoolbaritems

dynamic Binding to ToolbarItem Icon to switch Images from File


I am trying to build a Toolbar using MVVM with a Filter Icon. when you click it you navigate somewhere where you set a filter and then get navigated back. IF the Filter is active with selected Items I want the Toolbar Icon to be a different image now.

BUT the Binding doesn't seem to work. Whenever I set the Icon to just a string of my Image in files like "Filter.png" the Toolbar does only display the Text "FILTER". When I convert the value I bound to a FileImageSource I get the error

So how do I do this correctly? I already tried using a converter but same result. My code is:

XAML:

 <ContentPage.ToolbarItems>
    <ToolbarItem Order="Primary" Icon="{Binding FilterIconActive}" Text="Filter" Priority="0" Command="{Binding FilterCommand}" />
</ContentPage.ToolbarItems>

MyViewModel:

private string _filterIcon;

public string FilterIcon
{
    get => _filterIcon;
    set => SetProperty(ref _filterIcon, value);
}

                FilterIcon = _selectedFilter.Any() ? "FilterDone.png" : "Filter.png";

As I said already, changing the itemtype from string to FileImageSource since Icon is of type MenuItem.Icon doesn't work either. Thanks guys


Solution

  • I see that you are binding the FilterIconActive property whereas you are making the change in FilterIcon property.

    Another thing that I noticed is you are not using the INotifyPropertyChanged's event to notify your property that there was a change in its value.