Search code examples
wpfuser-controlsmvvm-lightsimpleioc

Mvvm light SimpleIoC in usercontrol


I hope someone can shine some light on best practise for my usecase.

I am using mvvm light and the simpleIoC container in a wpf usercontrol. I register my model and view models, dataservice and designtime service to it (very much according to mvvm light sample code)

The SimpleIoC container usage examples that I have seen seems to always treat the container as static / global for GalaSoft namespace.

But if I would create two instances of my WPF control in the same application I of course want each user control to have its own set of VMs and Model instance. So basically its own set of SimpleIoC registered instances. How would I best accomplish that when the default IoC container seems to be a static object?


Solution

  • How would I best accomplish that when the default IoC container seems to be a static object?

    Don't use the default container but create your own instance of the SimpleIoc class:

    User Control A:

    SimpleIoc containerA = new SimpleIoc();
    containerA.Register<ViewModel>();
    ...
    ViewModel vm = containerA.GetInstance<ViewModel46>();
    

    User Control B:

    SimpleIoc containerB = new SimpleIoc();
    ...