Creating UserControls with ViewModels in WPF

Do you think it is good practice to create a UserControl already with the ViewModel and deploy it together? Thus, the UserControl will be bound to the ViewModel from the warehouse.

It seems like a good idea to me, but I have never seen such a thing.

Thanks.

+2
source share
2 answers

The only thing you need to be careful with is that you want your datacontext to be open and accessible for binding to the controls where you use this control.

If you lock the data context, it will make sure that any bindings you make to the control must have the specified source.

+2
source

I personally do not see anything wrong with this idea. For a complex enough UserControl anyway, a ViewModel is required. However, I would make this (VM) as possible.

Indeed, I saw how some virtual machines were shipped with controls - in Swing (Java). For example, TableModel serves as a kind of VM for JTable . [But without real bindings, Swing VMs are pretty weak.]

+1
source

All Articles