0

I implemented INotifyPropertyChanged for a simple MVVM pattern. I have a class in model not implementing INotifyPropertyChanged and ObservableCollection<class> in my viewmodel.

I need to run different methods when different column(cell) values changed. I don't really want to implement INotifyPropertyChanged to the model if it is not absolutely necessary.

I tried this ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged) solution but couldn't managed to work it out.

//ViewModel
public class LimitsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

ObservableCollection<SingleLimit> _limitsclone= new ObservableCollection<SingleLimit>();
        public ObservableCollection<SingleLimit> LimitClone
        {
            get { return _limitsclone; }
            set
            {
                if (_limitsclone != value)
                    _limitsclone = value;
                OnPropertyChanged();
            }
        }
    }
//Model
public class SingleLimit
    {
        public string frstr{ get; set; }
        public double X{ get; set; }
        public double MaxP{ get; set; }
        public double MaxM{ get; set; }
    }
  • 4
    Implementing INotifyPropertyChanged is the standard approach, hence *absolutely necessary*. You may wrap the model class in a view model class with INPC and have an `ObservableCollection`. – Clemens May 22 '19 at 13:41
  • 1
    This isn't causing you any problems here, but it's worth knowing: In your setter, `OnPropertyChanged();` should be *inside* the `if`. That's all the `if` is there for: Just to avoid making a lot of UI refresh itself unnecessarily when the value hasn't actually changed. On your actual issue, ObservableCollection *only* raises events when items are added, removed, or swapped in the collection. It can't know what the items themselves are doing internally. The items are responsible for raising their own notifications when their own properties change. – 15ee8f99-57ff-4f92-890c-b56153 May 22 '19 at 14:11
  • @EdPlunkett Thanks Ed for the info. So you say i must implement `OnPropertyChanged();` in my model class and as @Clemens said i should wrap my model in my viewmodel? – Eren Akman May 22 '19 at 14:16
  • 1
    @ErenAkman The class in the observablecollection *must* be a viewmodel that fully implements INotifyPropertyChanged. *If you wish*, you can write a `SingleLimitViewModel` wrapper that provides an INPC wrapper property for each property of `SingleLimit` and use that as a wrapper around `SingleLimit`. Another option is just to turn `SingleLimit` into `SingleLimitViewModel` and not have a separate model class. If you're not passing the models back and forth to a database layer (other than EF, which implements INPC IIRC) or something, that's a valid approach. – 15ee8f99-57ff-4f92-890c-b56153 May 22 '19 at 14:21
  • BTW [the other question you linked in your question](https://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop) was trying to solve a different problem than yours. His collection items were raising the right notifications, but he wanted to handle those notification events himself. And then the guy who answered it misunderstood the documentation. – 15ee8f99-57ff-4f92-890c-b56153 May 22 '19 at 14:24
  • Personally, i prefer copying properties from an ef model class or a dto to a viewmodel over wrapping. Copying 4 properties is very little code If you have any substantial models take a look at automapper. Oh. And comparing objects can be tricky. What are the chances you'll set that collection to the same one? – Andy May 22 '19 at 15:57

0 Answers0