views:

74

answers:

1

I am using MVVM architecture.

I have a usercontrol UC as a View

Model is a ModelData class

ViewModel (UCViewModel) is binded to a usercontrol.

I have three more usercontrols that is inside the usercontrol UC ( discussed above).

Let's say uc1, uc2 and uc3.

and the visibility of uc1 , uc2 and uc3 inside UC depends on the type selected ( which ever radio button is selected ).

Since UC is binded to UCViewModel and I have to do all the stuff related to uc1 , uc2 and uc3 inside UCViewModel. Can I have seperate VM to uc1 , uc2 and uc3.. if Yes how can i do that ? Please Help!!

+3  A: 

As far as I understand your question, you can solve this by having UC expose a SelectedSubView (or whatever) property:

public object SelectedSubView { get; }

At the same time, you bind the radiobuttons to other properties of UC and update SelectedSubView accordingly (remember to implement INotifyPropertyChanged). Based on the selected radiobutton properties, the SelectedSubView must return the appropriate ViewModel.

You then bind a ContentPresenter to the SelectedSubView property and use DataTemplates to select the correct user controls (uc1, uc2 or uc3) based on the type of the current SelectedSubView.


Since you only want to hide inactive Views, it's probably best to keep their respective ViewModels around, so you may want to make them fields in the UC class

public class UC
{
    private MyFirstViewModel vm1;
    private MySecondViewModel vm2;
    private MyThirdViewModel vm3;
    private object selectedVM;

    public object SelectedSubView
    {
        get { return this.selectedVM; }
    }

    // This method should be called whenever one of the radio buttons
    // are updated (from their bindings)
    private void UpdateSelectedView()
    {
        this.selectedVM = // pick from vm1, vm2, vm3 according to radio button

        // Remember to raise INotifyPropertyChanged for SelectedSubView
    }
}
Mark Seemann
Thanks Mark for the reply.. Can you please give me an idea of returing a viewmodel or how to set the viewmodel at runtime?
Ashish Ashu
@Ashish Ashu: Updated my answer.
Mark Seemann