获取MVVM中属性的OldValue

时间:2016-10-21 19:11:20

标签: c# wpf mvvm

当我想保存时,我正试图获取MVVM中属性的旧值。 我想知道selectedItem的初始值,因为我想 1-知道项目是否已更改 2-使用字段UpdatedDate更新表 3-如果价格发生变化,我想更新另一张价格历史表。

我该怎么做

    public class ItemViewModel : ObjectBase
{
    public ObservableCollection<Item> itms = new ObservableCollection<Item>();

    private ObservableCollection<Item> _prevItems;



    private Item selectedItem;

    public Item SelectedItem
                {
                    get
                    {
                        return this.selectedItem;
                    }
                    set
                    {
                        this.selectedItem= value;
                        this.RaisePropertyChanged("SelectedItem");

                    }
                }
    #region SaveItem
    private void SaveItem(object obj)
        {
            if (this.items != _prevItems)
            {
                this.selectedItem.DateUpdated = this.DateUpdated;
                this.item.EditAsync(this.selectedItem );

            }
            else 
            {
                this.item.EditAsync(this.selectedItem );
            }

            this.LoadData();
          }
    #endregion
    #region Items
     private ObservableCollection<Item> items;

    public ObservableCollection<Item>  Items
    {
        get { return this.items; }
        set 
        {
            this.items = value;
            itms = items;
            this.RaisePropertyChanged("Items"); 
        }
    } 
    #endregion          
    #region EditItem
    private void EditItem(object obj)
    {
        itms = items;
        _prevItems = itms;

    }
    #endregion          
}

public class ObjectBase : INotifyPropertyChanged 
{       
    public event PropertyChangedEventHandler PropertyChanged;

    #region RaisePropertyChanged
    protected void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));               
        }
    }
    #endregion
}   

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

如果我理解下面可以提供帮助

public ObservableCollection<Item> itms
{
get{ return _itms;}
set
{
if(value != _itms)
{
_prevItems = _itms;
_itms = value;
}

}

这将在使用新值

更新之前保存旧项目