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?