0
votes

i've no clue how to inject a parameter in the constuctor call of the MainWindowViewModel. What i want is to register an instance of the current mainwindow (this) in the viewmodellocator. How can i accomplish this?

This is my situation in the viewmodellocator class:

public ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    //SimpleIoc.Default.Register<Window>();  //THIS IS NOT WORKING BECOUSE ITS A NEW INSTANCE, I NEED THE INSTANCE OF THE CURRENT MAINWINDOW HERE.
    SimpleIoc.Default.Register<MainWindowViewModel>();
}

MainViewModel:

public MainWindowViewModel(Window window)
{
    this.Window = window;

    //Listen out for the window resizing
    this.Window.StateChanged += StateChanged;
}

MainWindow.xaml

DataContext="{Binding MainWindow, Source={StaticResource Locator}}"
1
By the way, you're breaking the whole MVVM concept by providing a view reference to your view model. The view model shouldn't care about views.dymanoid

1 Answers

1
votes

If you want a Window to be resolved to your MainWindow:

public ViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

    SimpleIoc.Default.Register<System.Windows.Window>(() => System.Windows.Application.Current.MainWindow as MainWindow);
    SimpleIoc.Default.Register<MainViewModel>();
}

Also note that you should bind to just "Main" in your XAML:

DataContext="{Binding Main, Source={StaticResource Locator}}"