1

On custom property setter I try to call my custom event and pass NotifyCollectionChangedAction.Replace as parameter for the NotifyCollectionChangedEventArgs but I get an System.ArgumentException. What i'm doing wrong?

my custom event :

public event EventHandler<NotifyCollectionChangedEventArgs> MyEntryChanged;

protected virtual void OnMyEntryChanged(NotifyCollectionChangedEventArgs e)
{
    var handler = MyEntryChanged;
    handler?.Invoke(this, e);
}

and my call :

private TValue _value;

        public TValue Value
        {
            get { return _value; }
            set
            {
                if (Equals(_value, value)) return;
                _value = value;
                OnMyEntryChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace));
                OnPropertyChanged();
            }
        }
Manfred Radlwimmer
  • 12,469
  • 13
  • 47
  • 56

1 Answers1

2

The Argument Replace requires you to specify the old item and the new item. Simply invoking it without any items will cause this exception.

In your case you could invoke it like this:

if (Equals(_value, value)) return;

int indexOfPreviousItem = 0; //wherever you store your item
TValue oldItem = _value;    
_value = value;
OnMyEntryChanged(
    new NotifyCollectionChangedEventArgs(
        NotifyCollectionChangedAction.Replace, 
        value, 
        oldItem,
        indexOfPreviousItem));
OnPropertyChanged();
Manfred Radlwimmer
  • 12,469
  • 13
  • 47
  • 56