1

那是我第一个使用 MVVM , MVVM light 的项目。我有一个列表框,它从 PersonList Observable 集合中刷新,正常添加和删除刷新。问题是在编辑项目时。

我寻找了这个问题的所有解决方案,但没有任何效果,这让我觉得我错过了一些东西。

所以这里是代码:

 public class AdminViewModel : ApplicationPartBaseViewModel
{
   private ObservableCollection<Person> personList;

   public AdminViewModel()
    {

       this.context = new Entities();
       this.SavePersonCommand = new RelayCommand(() => this.SavePerson ());


       this.PersonList = new ObservableCollection<Peson>(context.Person.OrderBy(o => o.PersonName).ToList());


     }

      public ObservableCollection<Person> PersonList
    {
        get
        {
             return personList;
        }

        set
        {
            this.personList = value;
            RaisePropertyChanged("PersonList");
        }
    }


     private void SavePerson()
    {
      //Add and update code here
       this.context.SaveChanges();
       RaisePropertyChanged("PersonList");



    }


}

Person 类是从 DataModel edmx 自动生成的模板

 //------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

public partial class Person 
{
    #region Primitive Properties

    public virtual int PersonId
    {
        get;
        set;
    }

    public virtual string PersonName
    {
        get;
        set;
    }

    public virtual Nullable<int> PersonAge
    {
        get;
        set;
    }

    #endregion
    #region Navigation Properties

    public virtual ICollection<Humans> Humans
    {
        get
        {
            if (_human == null)
            {
                var newCollection = new FixupCollection<Human>();
                newCollection.CollectionChanged += FixupHuman;
                _human = newCollection;
            }
            return _human;
        }
        set
        {
            if (!ReferenceEquals(_human, value))
            {
                var previousValue = _human as FixupCollection<Human>;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupHuman;
                }
                _human = value;
                var newValue = value as FixupCollection<Human>;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupAssets;
                }
            }
        }
    }
    private ICollection<Human> _human;

    #endregion
    #region Association Fixup

    private void FixupHuman(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (Human item in e.NewItems)
            {
                if (!item.Person.Contains(this))
                {
                    item.Person.Add(this);
                }
            }
        }

        if (e.OldItems != null)
        {
            foreach (Human item in e.OldItems)
            {
                if (item.Person.Contains(this))
                {
                    item.Person.Remove(this);
                }
            }
        }
    }

    #endregion
}

我认为当我调用 RaisePropertyChanged 时 MVVM 灯会更新项目。我感到很困惑。

提前致谢。

4

1 回答 1

2

如果可以的话,第一个选项是尝试让您的自动生成的类来实现 INPC。看看Fody.PropertyChanged

如果这是不可能的,因为它确实具有“虚拟”的属性,我们可以在派生类中覆盖它们,例如

public class ObservablePerson : Person, INotifyPropertyChanged {
  public override int PersonId {
    get {
      return base.PersonId;
    }
    set {
      base.PersonId = value;
      OnPropertyChanged();
    }
  }

  public override string PersonName {
    get {
      return base.PersonName;
    }
    set {
      base.PersonName = value;
      OnPropertyChanged();
    }
  }

  public override int? PersonAge {
    get {
      return base.PersonAge;
    }
    set {
      base.PersonAge = value;
      OnPropertyChanged();
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  [NotifyPropertyChangedInvocator]
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
      handler(this, new PropertyChangedEventArgs(propertyName));
  }
}

现在在你的AdminViewModel工作中使用类型ObservablePersonPerson

于 2013-06-17T14:39:10.327 回答