1

My previous similarly related question was answered through using INotifyPropertyChanged. However, researches taught me that inheriting ViewModelBase from GalaSoft.MvvmLight is similar with INotifyPropertyChanged.

I was using this answer from my question in changing data in every item in ObservableCollection. But I don't want use INotifyPropertyChanged anymore since I am already inheriting ViewModelBase. Codes below are some that I added from the answer I already mentioned:

Food class

private bool _isAllSelected = true;

public bool IsAllSelected
{
    get
    {
        return _isAllSelected;
    }
    set
    {
        Set(IsAllSelected, ref _isAllSelected, value);
        // send message to viewmodel
        Messenger.Default.Send(Message.message);
    }
}

ViewModel class

// message handler
private void MsgHandler(Message message)
{
    RaisePropertyChanged(SelectAllPropertyName);
}

// the property that change all checkbox of fruits
public const string SelectAllPropertyName = "SelectAll";

public bool SelectAll
{
    set
    {
        bool isAllSelected = Foods.Select(c => c.IsAllSelected).Any();
        foreach (var item in Foods.SelectMany(c => c.Fruits).ToList())
        {
            item.IsSelected = isAllSelected;
        }
    }
}

// receives message function, called at the start
public void Receiver()
{
    Messenger.Default.Register<Message>(this, MsgHandler);
}

Problem here is this is not working like it was previously using INotifyPropertyChanged.

LindaSingh
  • 84
  • 1
  • 13

1 Answers1

1

You've mentioned that you are using the answer from your previous question and also this "I don't want use INotifyPropertyChanged anymore since I am already inheriting ViewModelBase" from this question.

You can actually remove the inheritance of INotifyPropertyChanged from your Fruit class (refer to previous question), as you can still use PropertyChangedEventHandler as long as you are using System.ComponentModel in your class usings.

So basically, this will be the only change from your previous question's answer code:

public class Fruit : ViewModelBase
{
    ....
}
CodeRed
  • 905
  • 1
  • 6
  • 24