Conditional View Model

Jan 27, 2014 at 6:20 PM
Edited Jan 27, 2014 at 6:26 PM
I have a module that is a user control of a data grid. I want this data grid to be able to take any view model that implements IFreq_BO. The grid will then display the right columns depending on what view model was passed in.

My ViewModel for the module:
 class FrequencyEditVM : ViewModelBase
        public FrequencyEditVM(ViewModelBase baseinterface, IFrequency_BO bointerface)

I have 5 menu options, all of which will open this window with this user control data grid. But what I want is to make it so each menu option will pass a different view model to the constructor of this VM. I want to only write the code once for the control of course. How would I go about doing this with prism?

So I guess the core issue is: Depending on which menu option the user selects, how do I CONDITIONALLY inject one of 5 view models?
Jan 28, 2014 at 12:50 PM
You would have to have your menu options instantiate the correct IFrequency_BO object they want to pass in. Then follow this post

Which allows you to send in dependency overrides. Code is based on using Unity as your DI.
Jan 28, 2014 at 1:28 PM
If I understood correctly, you would like to create a particular instance of FrequencyEditVM and display it with the corresponding ViewModelBase that depends on selecting any menu option.

One possible way of selecting the proper ViewModel would be by using the container which may be retrieved from the IModule class constructor. Each menu option would have an associated command, so each one would call the constructor and navigation of FrequencyEditVM, passing the corresponding ViewModel as follows:

NavigateToWindow(ViewModelBase viewModel)
    // Create FrequencyEditVM instance passing Base viewModel through parameter.
    // Open window and display FrequencyEditVM's View.
I hope this helped, Regards.

Gabriel Ostrowsky
Marked as answer by mathius25 on 1/28/2014 at 10:46 AM
Jan 28, 2014 at 3:48 PM
Edited Jan 28, 2014 at 6:45 PM
Ah, that makes perfect sense. Thank you so much! For some reason, when I am learning new frameworks, simple things seem convoluted while in the learning process. Thanks to both of you!