Mvvm:如何从另一个 ViewModel 更新我的 ObservableCollection<customobject>?

Mvvm: How to update my ObservableCollection<customobject> from another ViewModel?

我很难理解如何更新在 ViewModel1 中定义的 ViewModel2 中的 ObservableCollection。我已经搜索了很多有关如何执行此操作的信息,但无法得到正确的理解。我正在使用 MVVM Light 框架。

我有一个 DozentViewModel,它有一个 Dozent 的 ObservableCollection。 我有另一个 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 属性 绑定到我的 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 没有得到更新。我该怎么做呢?任何信息都会有很大帮助!!

谢谢, 维尼萨

您可以使用 MVVMLight 的 Messenger 进行视图模型通信。

例如:

public class AddDozentViewModel
{
    //... 

    private void AddDozent()
    {
        Dozent dozent = new Dozent();
        //...

        Messenger.Default.Send(new NewDozentAddedMessage(dozent));
    }
}

public class DozentViewModel
{
    //...

    private ObservableCollection<Dozent> _dozentList;
    public DozentViewModel()
    {
        Messenger.Default.Register<NewDozentAddedMessage>(this, HandleNewDozentAddedMessage);
    }
    private void HandleNewDozentAddedMessage(NewDozentAddedMessage obj)
    {
        _dozentList.Add(obj.DozentObject);
    }
}

public class NewDozentAddedMessage
{
    public Dozent DozentObject { get; }
    public NewDozentAddedMessage(Dozent dozentObject)
    {
        DozentObject = dozentObject;
    }
}

我在这里要做的是使 AddDozentViewModel 成为 DozentViewModel 的 属性,其中将包含对其父项的引用,例如:

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
}