如何阻止INotify更新两次?

时间:2017-03-27 14:09:23

标签: c# wpf mvvm datagrid inotifypropertychanged

我正在更新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的实体扩展程序类:

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代码并弹出两次。

老实说,我已经用这个打了一堵砖墙,正确方向上的任何一点都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

您可以使用一个标志来确定是否实际调用SaveMasterData方法。在将false属性设置为null之前将此标志设置为StaffNo,然后立即将其设置回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);
    }
}