This project has moved and is read-only. For the latest updates, please go here.

How to hide BaseViewModel properties from DataForm

Jan 27, 2011 at 11:05 PM

I have a VM (CompanyViewModel.cs) which is derived from BaseViewModel.  I want to use the DataForm to display a CompanyViewModel but when I do it the properties of BaseViewModel are also displayed (RegisteredViews, GoToVisualState, EventAggregator, Router and Logger).  What would be the best way to fix this?

I could modify BaseViewModel and add [Display(AutoGenerateField=false)] to each property but that adds a dependency on System.ComponentModel.DataAnnotations to Jounce.Core and also puts me out of step with any Jounce releases.

Is there some way to use the MetadataType attribute together with a specific metadata class to hide the BaseViewModel properties from the DataForm?

I could create another class, CompanyDisplayViewModel, and put all the editable fields in there and just have an instance of that class within CompanyViewModel.  This is doable but doesn't feel like the right approach.

I'm hoping that there is some way to tell DataAnnotations to ignore properties of a base class.

I realise that this is not, strictly, a Jounce problem but thought someone may have run in to this issue when using Jounce in the past.

Jan 31, 2011 at 12:52 PM

I understand the convenience of the data form but honestly I've found it works in 5% of the situations where you want something quick and dirty, but most of the time you need far more control over the fields and forms which is why I did not explicitly support it in Jounce.

Two options come to mind. One is you can create a branch project that creates a BaseEntityFormViewModel and copy the Entity view model and then place the attributes. This will avoid breaking the core and will simply need to be updated if the core is added to in future versions.

The other is to overload the properties with the new keyboard in your own class, pass through to the underlying properties but place the attributes there.

Eventually I think you'll find it is just as easy and you have far more control simply working with the fields and data-bindings without giving up the entire form to the DataForm control.