Pass paramaters to viewmodel constructor?

Topics: Prism v4 - WPF 4
Oct 8, 2010 at 1:20 AM

I have a listbox, that when an item is clicked, it will replace the region with a detail view of that record.
I am having a hard time figuring out how to pass the entity to the constructor of the viewmodel before activating the region.
This is what I have so far.

// RecordViewModel.cs
private void ShowEdit(TableA param)
    IRegion region = this.regionManager.Regions[RegionNames.MainRegion];

    object viewToActivate = region.GetView("DetailView");
    if (viewToActivate == null)
        var configurationViewModel = this.container.Resolve<IConfigurationViewModel>();

        // What goes here so I can pass param to ConfigurationViewModel?

        this.regionManager.Regions[RegionNames.MainRegion].Add(viewToActivate, "DetailView");


// ConfigurationViewModel.cs
public class ConfigurationViewModel : IConfigurationViewModel
    private TableA tableA;

    public ConfigurationViewModel(TableA tableA)
        this.tableA = tableA;


// WorkspaceAModule.cs
protected void RegisterViewsAndServices()
    this.container.RegisterType<IConfigurationViewModel, ConfigurationViewModel>();
Oct 8, 2010 at 2:35 PM
Edited Oct 8, 2010 at 2:36 PM


In Unity you could resolve a type and indicate its constructor parameters by using Overrides. Therefore, you code might look as shown below:

var viewModel = container.Resolve<IConfigurationViewModel>(new ParameterOverride("tableA", param));

For more information on this topic you could take a look at the following documentation section on MSDN:

 Hope this helps.

Fernando Antivero

Oct 8, 2010 at 5:31 PM

Thank you. That helped out a lot.

After getting it to work, I read that I should not be registering entities to the IoC container.

Do any of the quick starts show how to do this properly?

Oct 8, 2010 at 6:44 PM
Edited Oct 8, 2010 at 6:46 PM


Nice to see that you achieved your scenario.  Regarding to your question there is an specific section in the documentation of Prism (Prism4.chm) about this:

  • Chapter 3: Managing Dependencies Between Components (updated), see the Considerations for using the Container section

Additionally, you could obtain guidance about this taking a look at the the Stock Trader Reference Implementation.

Hope this helps.

Fernando Antivero