如何阻止 INotify 更新两次?

How to stop INotify from updating twice?

我正在更新一个 Datagrid,当用户输入一个已经存在的数字时,我想通知用户他们这个数字已经存在,然后从 datagrid 中清除该值。

我知道为什么会这样,但我不知道如何停止或如何解决。

这是非常简化的代码:首先将 EF 代码与 MVVM 模型结合使用。

public partial class StaffMasterData
{
    public System.Guid Id { get; set; } // ID (Primary key)        
    public int? StaffNo { get; set; } // StaffNo        

    public StaffMasterData()
    {
        InitializePartial();
    }

    partial void InitializePartial();        
}

StaffMasterData 的实体扩展 class :

public partial class StaffMasterData : INotifyPropertyChanged
{
    partial void InitializePartial()
    {
        Id = Guid.NewGuid();            
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

以及保存数据的方法:

public void SaveMasterData(StaffMasterData nwRowData)
{
    using (var db = CreateDbContext())
    {
        //MasterDataBinding is the observableCollection
        //the datagrid is being bound to.

        var staffNoExists = MasterDataBinding.Any(p => p.StaffNo == nwRowData.StaffNo);
        if (!staffNoExists)
        {
            db.StaffMasterDatas.AddOrUpdate(nwRowData);
            db.SaveChanges();
        }
        else
        {
            Alerts.Error("Staff Number exists");
            nwRowData.StaffNo = null;
        }
    }
}

以及集合更改事件的分配:

public class ShiftManagerViewModel : INotifyPropertyChanged
{
    private ObservableCollection<StaffMasterData> _mMasterDataBinding = new ObservableCollection<StaffMasterData>();

    public ObservableCollection<StaffMasterData> MasterDataBinding
    {
        get { return _mMasterDataBinding; }
        set
        {
            if (value != _mMasterDataBinding)
            {
                _mMasterDataBinding = value;
                OnPropertyChanged();                    
            }
        }
    }


    public ShiftManagerViewModel()
    {          
        _mMasterDataBinding.CollectionChanged += collectionChanged_Event;
    }


    private void collectionChanged_Event(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null && e.NewItems.Count > 0)
        {
            foreach (INotifyPropertyChanged item in e.NewItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged += propertyChanged_Event;
            }
        }
        if (e.OldItems != null && e.OldItems.Count > 0)
        {
            foreach (INotifyPropertyChanged item in e.OldItems.OfType<INotifyPropertyChanged>())
            {
                item.PropertyChanged -= propertyChanged_Event;
            }
        }
    }

    public void propertyChanged_Event(object sender, PropertyChangedEventArgs e)
    {
        if (sender is StaffMasterData)
        {
            SaveMasterData((StaffMasterData)sender);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

可能很清楚,当 运行 通过这行代码 nwRowData.StaffNo = null; 时,它再次触发事件,因为集合已被修改,然后依次运行 messageBox 代码,它会弹出两次。

老实说,我遇到了困难,如果方向正确,我将不胜感激。

您可以使用一个标志来确定是否实际调用 SaveMasterData 方法。在将 StaffNo 属性 设置为 null 之前将此标志设置为 false,然后立即将其设置回 true

private bool _handle = true;
public void SaveMasterData(StaffMasterData nwRowData)
{
    using (var db = CreateDbContext())
    {
        //MasterDataBinding is the observableCollection
        //the datagrid is being bound to.

        var staffNoExists = MasterDataBinding.Any(p => p.StaffNo == nwRowData.StaffNo);
        if (!staffNoExists)
        {
            db.StaffMasterDatas.AddOrUpdate(nwRowData);
            db.SaveChanges();
        }
        else
        {
            Alerts.Error("Staff Number exists");

            _handle = false;
            nwRowData.StaffNo = null;
            _handle = true;
        }
    }
}

public void propertyChanged_Event(object sender, PropertyChangedEventArgs e)
{
    if (!_handle && sender is StaffMasterData)
    {
        SaveMasterData((StaffMasterData)sender);
    }
}