0

I am a WPF application beginner and I'm currently using MvvmLight for my application.

I have a MainViewModel that holds a ObservableCollection of ChildViewModels(Type ViewModelBase). Each ChildViewModel is bound to a tab item in the XAML. So, I have a TabControl with tab items and each tab item has its own View and ViewModel.

This is my MainViewModel. For now, I have only one ChildViewModel which is DozentViewModel.

public class MainViewModel : ViewModelBase
{
    private ObservableCollection<ViewModelBase> _vmCollection;

    public ObservableCollection<ViewModelBase> VmCollection
    {
        get { return _vmCollection; }
        set
        {
            Set(ref _vmCollection, value);
            RaisePropertyChanged("VmCollection");
        }
    }

    private DozentViewModel _dozentviewmodel;

    public DozentViewModel Dozentviewmodel
    {
        get { return _dozentviewmodel; }
        set
        {
            Set(ref _dozentviewmodel, value);
        }
    }

    /// <summary>
    /// Initializes a new instance of the MainViewModel class.
    /// </summary>
    public MainViewModel(DozentViewModel dozentViewModel)
    {
        _dozentviewmodel = dozentViewModel;            
        VmCollection = new ObservableCollection<ViewModelBase>();
        VmCollection.Add(_dozentviewmodel);

    }

This is my ViewModelLocator code:

public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<MainViewModel>();
        SimpleIoc.Default.Register<DozentViewModel>();

    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public DozentViewModel DozentVM
    {
        get
        {
            return ServiceLocator.Current.GetInstance<DozentViewModel>();
        }
    }

I am trying to implement Dependency Injection by using Constructor Injection in the MainViewModel. The above code works but I am not sure if my implementation of DI is right. I would like to have an interface of IChildViewModel with which all my ChildViewModels will be implemented. If I use an interface like so, how can I achieve DI?

Vini
  • 13
  • 4
  • If`MainViewModel` needs to instantiate the children, then it should accept a factory that knows how to build them as an argument. For me to be any more specific, I would nee more information about how you want to use the children, how the get built, what the link is between the interface and the concrete class / specific parent class is, etc... – Bradley Uffner Dec 05 '17 at 13:22
  • Side note--ObservableCollection properties should be read only. –  Dec 05 '17 at 19:27
  • @BradleyUffner The MainViewModel is my parent class which takes children - DozentViewModel, StudentViewModel, CompanyViewModel as parameters. The child viewmodels are built using ViewModelBase. I just want my MainViewModel to create these child viewmodels and to have a ObservableCollection of them. My MainWindow has a Tab Control with 3 tab items which are bound to their respective child viewmodels. There is no link between the child viewmodels. Each is independant of each other. In that case, can I implement a factory? – Vini Dec 13 '17 at 12:09
  • @Will Ok. But in that case, how would I add my ViewModels to the ObservableCollection? – Vini Dec 13 '17 at 12:17
  • `MyObservableCollection.Add(new ViewModel);` The ***properties*** should be read only. The collection is what you read and write to. Don't swap out the entire thing for a new collection. –  Dec 13 '17 at 14:08
  • @Will Ok. Understood, Thanks for your help ! – Vini Dec 13 '17 at 16:06

0 Answers0