我很难理解如何在 ViewModel1 中定义的 ViewModel2 中更新 ObservableCollection。我已经搜索了很多关于如何做到这一点,但无法得到正确的理解。我正在使用 MVVM Light 框架。
我有一个 DozentViewModel,它有一个 ObservableCollection 的 Dozents。
我有另一个 AddDozentViewModel 用于通过 Entity Framework 将新的 Dozent 添加到数据库中。但是如何将新的 Dozent 添加到 DozentViewModel 中的 ObservableCollection 中?
这是我的 DozentViewModel:
public class DozentViewModel : ViewModelBase
{
private IDozentDB _dozentDB;
private ObservableCollection<Dozent> _dozentList;
private string _nachName;
private string _vorName;
private string _akadGrad;
public RelayCommand ShowAddDozentCommand { get; private set; }
public ObservableCollection<Dozent> DozentList
{
get { return _dozentList; }
set
{
Set(ref _dozentList, value);
RaisePropertyChanged("DozentList");
}
}
public DozentViewModel(IDozentDB dozentDB)
{
_dozentDB = dozentDB;
DozentList = new ObservableCollection<Dozent>();
// To get list of all Dozents
DozentList = _dozentDB.GetAllDozents();
ShowAddDozentCommand = new RelayCommand(ShowAddDozentViewExecute);
}
我已将 DozentList 属性绑定(bind)到我的 DozentView :
<DataGrid ItemsSource="{Binding DozentList,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" CanUserAddRows="False" IsReadOnly="True" Grid.Column="0" >
这是我的 AddDozentViewModel,它将新的 Dozent 添加到我的 SQL 数据库中。
public AddDozentViewModel(IDozentDB dozentDB)
{
_dozentDB = dozentDB;
// To Add Dozent details to Dozent DB
AddCommand = new RelayCommand(AddDozent, CanAddDozent);
}
public string NachName
{
get { return _nachName; }
set
{
Set(ref _nachName, value);
RaisePropertyChanged("NachName");
AddCommand.RaiseCanExecuteChanged();
}
}
public string VorName
{
get { return _vorName; }
set
{
Set(ref _vorName, value);
RaisePropertyChanged("VorName");
AddCommand.RaiseCanExecuteChanged();
}
}
public string AkadGrad
{
get { return _akadGrad; }
set
{
Set(ref _akadGrad, value);
RaisePropertyChanged("AkadGrad");
AddCommand.RaiseCanExecuteChanged();
}
}
private void AddDozent()
{
Dozent dozent = new Dozent();
dozent.DozentNachname = this.NachName;
dozent.DozentVorname = this.VorName;
dozent.AkadGrad = this.AkadGrad;
_dozentDB.Create(dozent);
Messenger.Default.Send(new NotificationMessage("CloseAddDozentView"));
}
private bool CanAddDozent()
{
return (!string.IsNullOrEmpty(NachName)) && (!string.IsNullOrEmpty(VorName)) && (!string.IsNullOrEmpty(AkadGrad));
}
}
我知道我只是将新的 Dozent 添加到数据库中,而不是更新 ObservableCollection。因此我在 DozentView 上的 DataGridView 没有得到更新。我该怎么做呢?任何信息都会非常有帮助!!
谢谢,
维尼塔
最佳答案
我希望在这里做的事情类似于制作 AddDozentViewModel
DozentViewModel
的属性(property),其中将包含对其父级的引用,例如:
public class AddDozentViewModel
{
private readonly DozentViewModel _parent;
...
public AddDozentViewModel(DozentViewModel parent, IDozentDB dozentDB)
{
_parent = parent;
_dozentDB = dozentDB;
}
...
private void AddDozent()
{
Dozent dozent = new Dozent();
dozent.DozentNachname = this.NachName;
dozent.DozentVorname = this.VorName;
dozent.AkadGrad = this.AkadGrad;
_dozentDB.Create(dozent);
_parent.DozentList.Add(dozent);
Messenger.Default.Send(new NotificationMessage("CloseAddDozentView"));
_parent.AddDozentViewModel = null; // this will dispose of the AddDozentViewModel
}
...
}
DozentViewModel
因此看起来像这样:public class DozentViewModel : ViewModelBase
{
...
private AddDozentViewModel _addDozentViewModel;
public AddDozentViewModel AddDozentViewModel
{
get { return _addDozentViewModel; }
set
{
Set(ref _addDozentViewModel, value);
RaisePropertyChanged("_addDozentViewModel");
}
}
...
}
另外,为了更加确定新的
Dozent
已写入数据库,所以我总是觉得我与数据库同步,我可能会考虑检索新的 Dozent
从数据库中添加到 DozentList
.所以这会改变 AddDozent()
方法类似于:private void AddDozent()
{
Dozent dozent = new Dozent();
dozent.DozentNachname = this.NachName;
dozent.DozentVorname = this.VorName;
dozent.AkadGrad = this.AkadGrad;
_dozentDB.Create(dozent);
Dozent newDozentFromDB = // retrieve it from _dozentDB here
_parent.DozentList.Add(newDozentFromDB);
Messenger.Default.Send(new NotificationMessage("CloseAddDozentView"));
_parent.AddDozentViewModel = null; // this will dispose of the AddDozentViewModel
}
关于wpf - Mvvm:如何从另一个 ViewModel 更新我的 ObservableCollection<customobject>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847977/