Should a Usercontrol have a ViewModel?
NO. Your UserControls should NOT have ViewModels designed specifically for them. This is, in fact, a code smell. It doesn’t break your application immediately, but it will cause you pain as you work with it.
What goes in the ViewModel?
The View and the ViewModel
- The view and the viewmodel communicate via data-binding, method calls, properties, events, and messages.
- The viewmodel exposes not only models, but other properties (such as state information, like the “is busy” indicator) and commands.
What do I need to bind to a ViewModel?
So we’ll need a view that represents just a single productviewmodel: Next we’ll need that design time viewmodel so we can view our XAML in design time: A bit boring, but it makes design time work! The design time view model so you can see this working in design time. It generates an easy random set of products.
What does binding a dependency property of a view to its ViewModel mean?
This manually sets the binding to a property “ViewMode” that is in my view model. So “ViewMode” exists in both the view and the view model and the manual binding will keep everything in sync. In my XAML where I use the control as a stand-alone view the XAML looks like and in my XAML where I use the control as a child control the XAML looks like
What should a ViewModel do in Stack Overflow?
The view’s code-behind may define UI logic to implement visual behavior that is difficult to express in XAML or that requires direct references to the specific UI controls defined in the view.
What should a thin ViewModel layer do?
A “thin” viewmodel layer is a set of viewmodels that expose your model objects directly to the views, meaning the views end up binding directly to properties on the model objects. This can work for things like simple, read-only views, but what if you want to have behavior associated with each object?