代码之家  ›  专栏  ›  技术社区  ›  Vini

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

  •  1
  • Vini  · 技术社区  · 7 年前

    我很难理解如何在ViewModel1中定义的ViewModel2中更新ObservableCollection。我已经搜索了很多关于如何做到这一点,但没有得到正确的理解。我正在使用MVVM轻型框架。

    我有一个DozentViewModel,它有一个可观察到的Dozent集合。 我有另一个AddDozentViewModel,用于通过实体框架向数据库添加新的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不会得到更新。我该怎么做?任何信息都会非常有用!!

    谢谢 维尼萨

    2 回复  |  直到 7 年前
        1
  •  0
  •   Chris Mack    7 年前

    我想在这里做的是 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 已写入数据库,因此我总是觉得自己与数据库同步,我可能会考虑检索新的 讲师 在将其添加到 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
    }
    
        2
  •  0
  •   Jan Paolo Go vikomall    7 年前

    您可以使用MVVMLight的 Messenger 用于viewmodel通信。

    例如:

    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;
        }
    }