1

在自定义属性设置器上,我尝试调用我的自定义事件并NotifyCollectionChangedAction.Replace作为参数传递,NotifyCollectionChangedEventArgs但我得到一个System.ArgumentException. 我做错了什么?

我的自定义事件:

public event EventHandler<NotifyCollectionChangedEventArgs> MyEntryChanged;

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

我的电话:

private TValue _value;

        public TValue Value
        {
            get { return _value; }
            set
            {
                if (Equals(_value, value)) return;
                _value = value;
                OnMyEntryChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace));
                OnPropertyChanged();
            }
        }
4

1 回答 1

2

ArgumentReplace要求您指定旧项目和新项目。简单地调用它而不使用任何项目将导致此异常。

在您的情况下,您可以像这样调用它:

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();
于 2016-07-22T12:16:30.053 回答