Исходя из вашего вопроса, вам нужно два изображения, заполненные и не заполненные.Сначала свойство isFavorite должно быть ложным, я думаю.В вашем триггере действия кнопки внутри ViewModel
private void ButtonActionForFavouriteTriggered(object seletecedButtonItem)
{
System.Diagnostics.Debug.WriteLine("favourite button clickked");
var selectedList = (Result)seletecedButtonItem;
if (selectedList.isFavorite == "Fav.png")
{
// update api or local DB whatever on here
selectedList.isUserFavorite = false;
selectedList.isFavorite = "unFav.png"; // this will update in UI - changes from favorite image into unfavorite image
}
else
{
// update api or local DB whatever on here
selectedList.isUserFavorite = true;
selectedList.favourite = "Fav.png"; // this will update in UI
}
}
Здесь 'Result' - моя модель, а внутри вашей модели вы можете использовать интерфейс INotifyPropertyChanged, как этот.
public class Result: INotifyPropertyChanged
{
public int id { get; set; }
public string country { get; set; }
public string name { get; set; }
public bool isUserFavorite { get; set; }
//Note: If you want to change any thing without reload cells, use this type and inherit 'INotifyPropertyChanged'
// Example: Favourite and UnFavourite button action, tapping any event change any text from cell
public string abbr {
get { return _abbr; }
set { _abbr = value; OnPropertyChanged("abbr"); }
}
public string area { get; set; }
public string largest_city { get; set; }
public string capital { get; set; }
public string isFavourite
{
get { return _fav; }
set {
_fav = value; OnPropertyChanged("isFavourite");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public string _fav;
public string _abbr;
private void OnPropertyChanged(string v)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
}
}
и в xaml,внутри списка или другого;
<Button Grid.Column="3" x:Name="buttonFavourite" BackgroundColor="Transparent" Image="{Binding isFavourite}" Command="{Binding Source={x:Reference MyCollectionPage}, Path=BindingContext.FavouriteButtonAction}" CommandParameter="{Binding .}"/>