WPF (MVVM): Posting a viewModel to view code in a Window?

My legs are wet with the MVVM pattern for WPF, and everything works as it should.

But I am connecting the viewmodel to my datacontext view from my code in my window.

Of course, the point of MVVM is to get rid of the code in the windows ... that it does perfectly, so I was left with two simple lines to connect my datacontext to my view model.

Is there an alternative?

Or is it acceptable to have only 2 lines to connect the Viewmodel in my window code?

Is it possible to connect a viewmodel to a view via XAML? I think this is probably (unsure). But will this cause problems? Is this recommended?

Really appreciated any help?

+3
source share
1 answer

Yes, you can connect a DataContext to a view through XAML through DataTemplates. You can see an example of this in the excellent and recommended WPF Application article with Josh Smith's Model-View-ViewModel design pattern .

+3
source

Source: https://habr.com/ru/post/1718638/


All Articles