gpt4 book ai didi

wpf - 同样,ObservableCollection不会更新项目

转载 作者:行者123 更新时间:2023-12-03 10:28:59 26 4
gpt4 key购买 nike

那就是我的第一个使用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");



}


}

人员类是从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
}

我以为MVVM light在我调用RaisePropertyChanged时会更新该项目。
我感到很困惑。

提前致谢。

最佳答案

第一种选择是,如果可以的话,尝试使您自动生成的类实现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中使用 ObservablePerson类型而不是 Person类型的对象

关于wpf - 同样,ObservableCollection不会更新项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com