How to add validation to view model properties or how to implement INotifyDataErrorInfo

The preferred way since .Net 4.5 to implement data validation is to let your view model implement INotifyDataErrorInfo (example from Technet, example from MSDN (Silverlight)). Note: INotifyDataErrorInfo replaces the obsolete IDataErrorInfo. The new framework infrastructure related to the INotifyDataErrorInfo interface provides many advantages like support of multiple errors per property custom error objects and customization … Read more

Why should I use view models?

Where do I put my validation code? On the view models you should validate everything that’s specific to the application like for example the date should be in en-US format culture, …. I need to add code to map between business objects and view models. That’s why there are tools such as AutoMapper. Different problems … Read more

Should a user control have its own view model?

Absolutely, positively 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. A UserControl is simply an easy way to create a Control using composition. UserControls are still Controls, and … Read more

Android ViewModel additional arguments

You need to have a factory class for your ViewModel. public class MyViewModelFactory implements ViewModelProvider.Factory { private Application mApplication; private String mParam; public MyViewModelFactory(Application application, String param) { mApplication = application; mParam = param; } @Override public <T extends ViewModel> T create(Class<T> modelClass) { return (T) new MyViewModel(mApplication, mParam); } } And when instantiating the … Read more

ViewModel with List and editor templates

Assuming you have the following models: public abstract class TableInputModel { } public class RectangleTableInputModel : TableInputModel { public string Foo { get; set; } } public class CircleTableInputModel : TableInputModel { public string Bar { get; set; } } And the following controller: public class HomeController : Controller { public ActionResult Index() { var … Read more