Hello to all.

I decided to wear out myself with WPF. And of course, the MVVM pattern :)

Of course, I immediately ran into a problem, how to open a UserControl using the MVVM pattern?

Of course, no problem to do it in the View itself. But what about MVVM. Someone can give an example in a couple of lines of code How do I implement this. the fact is that on the Internet there are many descriptions of the MVVM pattern, all are limited to writing a calculator, I mean on one form. In principle, I also always write on the same form, but at the right time I substitute the control. emitting a transition to another form. I do not know whether it is right or not. but I feel so comfortable.

  • What does "open control" mean? - Alexey Sarovsky
  • @ Alexey Sarovsky Yes, probably, it was more correct to describe the principle of the program. The program starts. A form opens on which UserControl is placed, for example, Greetings and program descriptions. then the user presses the button further and the next userControl opens, say, with the registration form, etc. At the same time, of course, the control with a greeting closes - Gennady Pisarev
  • Everything, understood you. Hmm ... this is somehow not very Orthodox, in my opinion, on one form to change UserControls. Unless to place on the form all UserControl, to put Visible in false at necessary and this Visible to tie to ViewModel. It already sets the Visible to true / false as needed - but the crutch is hard too. And you can find out why 1 form exactly? - Alexey Sarovsky
  • I don’t even know myself :). I wrote applets on WInForm. So I was too lazy every time to create a new form. Make settings for her and all that. And so there is one Form, it can be said that the design of the program is one, you just support the new controls and everything in a bundle. Honestly, I am quite weak in programming. I just wonder I'm self-taught. despite the fact that I wrote a program for my friend. Which has its own billiard bar. so I gave him a program that counted for how long his client was playing at the table and for how much he divided orders - Gennady Pisarev
  • If you can, then tell me how. - Gennady Pisarev

1 answer 1

Popular way is this.

You prescribe

class MainVM : INotifyPropertyChanged { VM activePageVM; public VM ActivePageVM { get { return activePageVM; } set { activePageVM = value; NotifyPropertyChanged(); } } // обвеска для INotifyPropertyChanged } 

in the usual way. Now, your window looks like this:

 <Window ...> <ContentPresenter Content="{Binding ACtivePageVM}"/> </Window> 

(You can even add around the total body kit needed on all pages).

It remains to make sure that your ContentPresenter selects the correct UserControl depending on the type of VM. This is made obvious by dispatching via DataTemplate :

 <DataTemplate DataType="{x:Type vm:FirstVM}"> <ui:FirstUserControl/> </DataTemplate> <DataTemplate DataType="{x:Type vm:SecondVM}"> <ui:SecondUserControl/> </DataTemplate> 

Place this dispatch in App.xaml or somewhere else where it is visible in the main window. If you need to locally block the display somewhere for some type, use local resources, they always take precedence.

  • @VlalD Thanks for the reply. Everything seems clear. Only I get the error NotifyPropertyChanged (); - Gennady Pisarev
  • Well, this is a sketch of the code. You, of course, need to implement INotifyPropertyChanged standard way. You still have to swear at the dots in the Window . And for DataTemplate you will need to specify the namespaces vm and ui . - VladD
  • @VlaID Naturally, I added a namespace. Otherwise, the INotifyPropertyChanged interface - Gennady Pisarev
  • @Gennady Pisarev: See how it is implemented, for example, here ( DemoCustomer class). - VladD