开发者

Query on MVVM design pattern on WPF

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!!


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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜